由于mod重写htaccess规则,嵌套网址上出现500个服务器错误,而不是404错误 [英] 500 Server error instead of 404 on nested urls due to mod rewrite htaccess rule(s)

查看:82
本文介绍了由于mod重写htaccess规则,嵌套网址上出现500个服务器错误,而不是404错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有自定义.htaccess文件的网站,该网站处理的一些事情:



1)它将不带 .php扩展名的网址视为具有 .php的网址



2)重定向 http:// http: // www。链接到 https:// www。



.htaccess文件:

  ErrorDocument 404 /404.php 

选项-MultiViews
RewriteEngine在

##上添加www并按照相同的规则打开https
RewriteCond%{HTTP_HOST}!^ www\。 [NC,OR]
RewriteCond%{HTTPS}!on
RewriteCond%{HTTP_HOST} ^(?: www\。)?(。+)$ [NC]
RewriteRule ^ https ://www.%1% {REQUEST_URI} [R = 301,L,NE]

#如果没有目录和.php文件,则添加.php扩展名
RewriteCond% {REQUEST_FILENAME}!-d
RewriteCond%{REQUEST_FILENAME} .php -f
RewriteRule ^(。+?)/?$ $ 1.php [L]

一切正常,但是我观察到一些奇怪的行为导致500个错误而不是404错误:



1)当您访问不存在的根级别URL时,例如 https://www.example.com / skjdfhkj 会按预期重定向到 404



2)当您访问不存在的嵌套网址,例如 https://www.example.com/some-text/skjdfhkj 其中 some-text 与任何现有的php文件都不匹配,则它重新会按预期将 404 转换为

3)。但是,当您访问某些不存在的嵌套url如 https://www.example.com/some-existing-page-name/skjdfhkj ,其中 some-existing-page-name 匹配现有php文件的名称( https://www.example.com/some-existing-page-name.php ),则给出 500服务器错误



我的问题是:如何更新htaccess以正确返回 404 而不是 500 ,当有人访问不存在的嵌套URL(例如 https://www.example.com/some-existing-page-name/skjdfhkj (其中 some-existing-page-name 与现有的php文件( https://www.example.com/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错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆