0

I have my custom error page at /errors/404.php and I would like that to be able to be used when apache can't find a file, but I would like it not able to be used by simply requesting the URL http://mywebsite/error/404.php.

I currently have a rule that blocks access to all of my php files (because I use URL rewriting to make user-friendly URLs) but this seems to also be stopping apache from reading the error page due to the error page matching the rule to deny access to php files.

How would I do this properly?

The contents of /.htaccess:

RewriteEngine On
RewriteRule ^home/?$       /index.php   [NC,L]
RewriteRule ^lessons/?$    /lessons.php [NC,L]

# Don't allow access to the actual php files.
RewriteCond %{THE_REQUEST} \.php[\ /?].*HTTP/ [NC]
RewriteRule ^.*$           -                  [R=404,L]

ErrorDocument 404 /errors/404.php
4

1 回答 1

1

你可以试试这个:

# Don't allow access to the actual php files.
RewriteCond %{THE_REQUEST} \.php[\ /?].*HTTP/  [NC]
RewriteCond %{REQUEST_URI}  !/errors/404\.php  [NC]
RewriteRule ^.*$  -        [R=404,L]

RewriteCond %{THE_REQUEST} \s/errors/404\.php [NC]
RewriteRule .?  -  [F]

ErrorDocument 404 /errors/404.php
  1. 所有对文件的请求.php都被重定向到/errors/404.php脚本。
  2. 对不存在的文件或目录的所有请求都被重定向到/errors/404.php脚本(该ErrorDocument 404 /errors/404.php指令适用于这种情况)。
  3. /errors/404.php返回 403 Forbidden 状态代码的所有直接请求。
于 2013-04-12T08:51:10.353 回答