PHP文件访问和控制

时间:2014-08-13 18:47:26

标签: php .htaccess file-access

我知道.htaccess是如何运作的,我无法决定,写些什么规则......

我选择了Zend框架的规则:

RewriteEngine On
# The following rule tells Apache that if the requested filename
# exists, simply serve it.
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
# The following rewrites all other queries to index.php. The
# condition ensures that if you are using Apache aliases to do
# mass virtual hosting, the base path will be prepended to
# allow proper resolution of the index.php file; it will work
# in non-aliased environments as well, providing a safe, one-size
# fits all solution.
RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteRule ^(.*)$ %{ENV:BASE}index.php [NC,L]

正如它所说的那样,所有不存在的东西都会被重定向到index.php,之后我会处理所请求的uri,但是那些只包含我保留的类的php文件呢?业务逻辑,没关系 - 用户将收到空白页面,但我想知道如何在访问时显示错误页面或更好,如何将其排除在访问之外?

2 个答案:

答案 0 :(得分:0)

您可以在RewriteEngine On行:

下方插入此规则
# if request is for a .php file but not index.php then return forbidden
RewriteCond %{REQUEST_URI} !^/index\.php [NC]
RewriteRule \.php$ / [L,NC,R]

答案 1 :(得分:0)

您可以使用以下设置进行检查:

ErrorDocument 404 http://www.example.com/error/404.html 

注意:您需要提供404.html的路径。如果您使用的是任何框架,那么很可能您会在错误模型目录中找到404.html文件。