问题描述
我有一个带有自定义.htaccess文件的网站,该网站处理的一些事情:
1)它将不带 .php扩展名的网址视为具有 .php的网址
2)重定向
3)。但是,当您访问某些不存在的嵌套url如,其中 some-existing-page-name 匹配现有php文件的名称(),则给出 500服务器错误。
我的问题是:如何更新htaccess以正确返回 404 而不是 500 ,当有人访问不存在的嵌套URL(例如(其中 some-existing-page-name 与现有的php文件())吗?
我想这与mod重写有关,mod重写没有 .php即可处理网址/ code>扩展名,就好像它具有 .php ,但不知道如何修改htaccess使其正常工作:(
尝试以此更改最后一条规则:
#如果没有目录和.php文件,则添加.php扩展名
RewriteCond%{REQUEST_FILENAME}!-d
RewriteCond%{DOCUMENT_ROOT} / $ 1.php -f
RewriteRule ^(。+?)/?$ $ 1.php [L]
%{REQUEST_FILENAME} 有时可以使用部分匹配来提供来自文件系统的意外匹配。
I have a site with custom .htaccess file that handles few things:
1) It treats urls without ".php" extensions as if it has ".php" in the end.
2) It redirects http:// and http://www. urls to https://www.
Here is the .htaccess file:
ErrorDocument 404 /404.php Options -MultiViews RewriteEngine On ## add www and turn on https in same rule RewriteCond %{HTTP_HOST} !^www\. [NC,OR] RewriteCond %{HTTPS} !on RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC] RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE] # if not a directory and .php file is present then add .php extension RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}.php -f RewriteRule ^(.+?)/?$ $1.php [L]
Everything works as expected, but I have observed some strange behavior that resulted in 500 errors instead of 404:
1) When you visit non-existed root level url such as https://www.example.com/skjdfhkj it redirects to 404 as expected.
2) When you visit non-existed nested url such as https://www.example.com/some-text/skjdfhkj where some-text does not match any existing php files, then it returns 404 as expected.
3) However, when you visit some non-existed nested url such as https://www.example.com/some-existing-page-name/skjdfhkj , where some-existing-page-name matches the name of existing php file (https://www.example.com/some-existing-page-name.php), then it gives out a 500 Server Error.
My question is: how do I update my htaccess to properly return a 404 instead of 500 when someone visits non-existing nested url such as https://www.example.com/some-existing-page-name/skjdfhkj (where some-existing-page-name matches the name of existing php file (https://www.example.com/some-existing-page-name.php)) ?
I guess it has something to do with mod rewrite that treats urls without .php extensions as if it had .php, but don't know how to modify htaccess to make it work properly :(
Try changing last rule as this:
# if not a directory and .php file is present then add .php extension RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{DOCUMENT_ROOT}/$1.php -f RewriteRule ^(.+?)/?$ $1.php [L]
%{REQUEST_FILENAME} sometimes can give unexpected matches from filesystem using partial matches.
这篇关于由于mod重写htaccess规则,嵌套网址上出现500个服务器错误,而不是404错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!