Custom error page failed to load after editing htaccess

折月煮酒 提交于 2019-12-25 07:30:02

问题


I edited my htaccess file to rewrite mysite.com/page to mysite.com/page.php and to show 404 error page if a user requested mysite.com/page.php instead of mysite.com/page

I also added my own custom 404 error page

But when I requested mysite.com/page.php, the error page shown was Apache default 404 error page (instead of my custom error page), with additional error info:

Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.

My error log:

Request exceeded the limit of 10 internal redirects due to probable configuration error.

Here is my htaccess file:

RewriteEngine On
RewriteBase /

RewriteRule ^([^.?]+)$ %{REQUEST_URI}.php [L]

RewriteCond %{THE_REQUEST} "^[^ ]* .*?\.php[? ].*$"
RewriteRule .* - [L,R=404]

ErrorDocument 404 /error/404

I have tried changing 404 at the last line above to 404.php but the custom error page still not showing up..

However, the custom error page did show up when I requested mysite.com/page/ (notice the slash).


回答1:


I don't understand, where this error comes from. But you can resolve it by exiting the rule chain when 404.php is requested. Insert this rule at the beginning

RewriteRule ^error/404.php$ - [L]

Unrelated, but you can simplify your rules a little bit

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_FILENAME}.php [L]

RewriteCond %{THE_REQUEST} \.php
RewriteRule \.php$ - [L,R=404]



回答2:


RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php
ErrorDocument 404 /error/404.php


来源:https://stackoverflow.com/questions/38171766/custom-error-page-failed-to-load-after-editing-htaccess

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!