PHP文件访问和控制


PHP file access and control

我知道.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文件,只包含类,我保持我的业务逻辑,这是对的-用户将收到空白页,但我想知道如何显示错误页时,它被访问或更好,如何排除它访问?

您可以将此规则插入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]

您可以通过以下设置检查:

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

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