htaccess 中的自定义 404 错误处理程序不适用于不存在的“.php”文件
Custom 404 error handler in htaccess not working for non-existent ".php" files
我正在使用自定义错误处理系统。
示例 .htaccess
文件命令:
ErrorDocument 404 /error-deal.php?code=404
如果我直接输入 URL,error-deal.php
文件存在并处理 $_GET 变量。
带有文件扩展名的页面不会触发 .htaccess
重定向到错误页面:
https://example.com/does-not-exist.php
然而 URL 没有扩展名...
https://example.com/does-not-exist
我在这里错过了什么?
其他重写规则是:
#force https
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NC]
#redirect all www to non www
RewriteCond %{HTTP_HOST} ^www.donbur.co.uk$
RewriteRule (.*) https://donbur.co.uk/ [R=301,L]
#URL re-writes for dynamic pages
#to handle all news requests
RewriteRule ^news/([^/]*)$ /gb-en/news/article.php?title= [L]
#to handle all feature requests
RewriteRule ^features/([^/]*)$ /gb-en/features/feature.php?title= [L]
Pages with file extensions don't trigger the htaccess redirect to the error page:
如评论中所述,这似乎只影响具有 .php
扩展名的 URL。这可能是由于在您的服务器上实施 PHP 的方式所致。例如,如果所有 *.php
请求都代理到备用后端进程,那么这将绕过应用程序服务器上的 .htaccess
文件。
不幸的是,由于这似乎是一个共享服务器,您可能无法控制它。
但是,您可以尝试一些方法。尽管您似乎在整个站点中使用以 .php
结尾的网址这一事实阻碍了这一点。
尝试重置默认错误文档然后设置您的自定义错误文档。 ErrorDocument
应该覆盖以前的设置,但是,有时当它在服务器配置中定义时,它不会。例如:
ErrorDocument 404 default
ErrorDocument 404 /error-deal.php?code=404
如果不是因为您在网站上使用了 .php
网址(尽管令人困惑的是,您的网站似乎混合了 .php
和无扩展名的网址?)那么您可能会使用 mod_rewrite 将所有 .php
请求强制为 404(尽管这仍然可能不会覆盖默认错误响应)。
您可以在 规范重定向(HTTP 到 HTTPS 和 www 到 non-www)后尝试以下:
# Force a 404 for ".php" requests that don't exist
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule \.php$ - [R=404]
如果您没有任何 .php
网址,那么您可以删除前面的 RewriteCond
指令。
旁白: 您应该确保 /error-deal.php
文档本身不可直接访问。目前 returns non-errors 的 200 OK 状态似乎没有任何东西(机器人元标记、响应 header、robots.txt
)阻止搜索引擎 crawling/indexing 此页。
我正在使用自定义错误处理系统。
示例 .htaccess
文件命令:
ErrorDocument 404 /error-deal.php?code=404
如果我直接输入 URL,error-deal.php
文件存在并处理 $_GET 变量。
带有文件扩展名的页面不会触发 .htaccess
重定向到错误页面:
https://example.com/does-not-exist.php
然而 URL 没有扩展名...
https://example.com/does-not-exist
我在这里错过了什么?
其他重写规则是:
#force https
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NC]
#redirect all www to non www
RewriteCond %{HTTP_HOST} ^www.donbur.co.uk$
RewriteRule (.*) https://donbur.co.uk/ [R=301,L]
#URL re-writes for dynamic pages
#to handle all news requests
RewriteRule ^news/([^/]*)$ /gb-en/news/article.php?title= [L]
#to handle all feature requests
RewriteRule ^features/([^/]*)$ /gb-en/features/feature.php?title= [L]
Pages with file extensions don't trigger the htaccess redirect to the error page:
如评论中所述,这似乎只影响具有 .php
扩展名的 URL。这可能是由于在您的服务器上实施 PHP 的方式所致。例如,如果所有 *.php
请求都代理到备用后端进程,那么这将绕过应用程序服务器上的 .htaccess
文件。
不幸的是,由于这似乎是一个共享服务器,您可能无法控制它。
但是,您可以尝试一些方法。尽管您似乎在整个站点中使用以 .php
结尾的网址这一事实阻碍了这一点。
尝试重置默认错误文档然后设置您的自定义错误文档。 ErrorDocument
应该覆盖以前的设置,但是,有时当它在服务器配置中定义时,它不会。例如:
ErrorDocument 404 default
ErrorDocument 404 /error-deal.php?code=404
如果不是因为您在网站上使用了 .php
网址(尽管令人困惑的是,您的网站似乎混合了 .php
和无扩展名的网址?)那么您可能会使用 mod_rewrite 将所有 .php
请求强制为 404(尽管这仍然可能不会覆盖默认错误响应)。
您可以在 规范重定向(HTTP 到 HTTPS 和 www 到 non-www)后尝试以下:
# Force a 404 for ".php" requests that don't exist
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule \.php$ - [R=404]
如果您没有任何 .php
网址,那么您可以删除前面的 RewriteCond
指令。
旁白: 您应该确保 /error-deal.php
文档本身不可直接访问。目前 returns non-errors 的 200 OK 状态似乎没有任何东西(机器人元标记、响应 header、robots.txt
)阻止搜索引擎 crawling/indexing 此页。