如果 URl 不存在,mod_rewrite .htaccess 会导致 500 服务器错误 [英] mod_rewrite .htaccess causing 500 server error if URl does not exist
问题描述
我刚刚开始使用 mod_rewrite,这是我用于支持多语言的基本结构化网站的方法:
I've just started using mod_rewrite, this is what I use for a quite basic structured website with multiple language support:
RewriteEngine on
ErrorDocument 404 /error404.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z]{2})/([^/]+)$ $2.php?lang=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z]{2})/$ index.php?lang=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z]{2})$ index.php?lang=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ $1.php [L]
我的想法是语言在 URl 的开头指示并且需要是两个字符(a-z 或 A-Z).之后将有一个字符串指向一个 php 文件,该文件与字符串具有相同的文件名,只是附加了 .php.语言将作为 GET 变量 (?lang=en
) 添加.
My idea was that languages are indicated at the beginning of the URl and need to be two characters (a-z or A-Z). After that there will be a string that refers to a php-file which has the same filename as the string, just .php attached. The language will be added as a GET-Variable (?lang=en
).
所以,一个 URl 可能看起来像这样:/en/index
然后应该被重定向到 index.php?lang=en
.
So, an URl could look like this: /en/index
and should then be redirected to index.php?lang=en
.
如果 URl 不是以语言开头,而是直接以字符串开头,则不会将 GET 变量附加到字符串.例如./index
应该是指index.php
.如果没有字符串,则应使用索引作为默认值.
If the URl does not start with a language, but directly with the string, then no GET-variable will be attached to the string. E.g. /index
should refer to index.php
. If there is no string, then index should be used as default.
到目前为止,一切都很好.工作正常.只要我输入一个字符串(无论我是否使用 2 个字符作为语言),该站点总是显示 500 内部服务器错误,而不是转到 error404.php
.另外,如果我删除 .htaccess 中的这一行(404),它仍然是同样的错误.
So far, so good. Works fine. Just if I enter a string (no matter if I use 2 characters for language or not), the site always shows an 500 Internal Server Error, instead of going to error404.php
. Also, if I delete this line (404) in .htaccess, it is still the same error.
所以,我认为 .htaccess 中的其他行有问题导致此错误,有人知道这可能是什么吗?
So, I assume that there is something wrong with the other lines in the .htaccess that cause this error, does anybody have an idea what this could be?
非常感谢任何帮助!
推荐答案
你的规则是循环的.乍一看,它看起来像你的最后一个:
Your rules are looping. At a glance, it looks like your last one:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ $1.php [L]
假设我有这个 URL,http://your.domain.com/blah
,并且文件 blah.php
不存在.这就是正在发生的事情.
Say I have this URL, http://your.domain.com/blah
, and the file blah.php
doesn't exist. This is what's happening.
- URI =
blah
, !-f 是真的,它不是文件 - URI =
blah
, !-d 是真的,它不是目录 - URI =
blah
,内部重写为blah.php
blah
!=blah.php
,重写规则循环- URI =
blah.php
!-f 是真的,它不是文件 - URI =
blah.php
!-d 是真的,它不是目录 - URI =
blah.php
,内部重写为blah.php.php
blah.php
!=blah.php.php
,重写规则循环
- URI =
blah
, !-f is true, it's not a file - URI =
blah
, !-d is true, it's not a directory - URI =
blah
, internal rewrite toblah.php
blah
!=blah.php
, rewrite rules loop- URI =
blah.php
!-f is true, it's not a file - URI =
blah.php
!-d is true, it's not a directory - URI =
blah.php
, internal rewrite toblah.php.php
blah.php
!=blah.php.php
, rewrite rules loop
这种情况一直持续到重写引擎受够了并返回 500 服务器错误为止.
This goes on until the rewrite engine has had enough and returns a 500 server error.
您可以在此处执行以下两项操作之一:
You can do one of 2 things here:
添加一个指令,使无论如何都停止所有循环,这很容易解决这类问题.但它会破坏需要循环的规则.我在你的规则中没有看到类似的东西,所以这样做是安全的(至少现在是这样).只需将此添加到您的规则的顶部:
Add a directive to make all looping stop no matter what, which is an easy to get around this kind of stuff. But it will break rules that require looping. I don't see anything like that in your rules so it's safe (at least for now) to do this. Just add this to the top of your rules:
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
先检查一下php文件是否真的存在:
Do a pre-emptive check to see if the php file actually exists:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^/]+)$ $1.php [L]
这篇关于如果 URl 不存在,mod_rewrite .htaccess 会导致 500 服务器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!