500来自不存在的文件/文件夹的错误

时间:2013-06-30 13:49:44

标签: .htaccess mod-rewrite

每当我尝试使用我的.htaccess访问不存在的文件/文件夹时,它会返回500错误而不是404错误并在日志中显示以下消息:

Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.

以下是导致问题的代码:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/*$ /$1.php?p=$2 [L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/*$ /$1.php [L]
</IfModule>

代码转换网址如下:

http://mysite.com/about -> http://mysite.com/about.php
http://mysite.com/something/else -> http://mysite.com/something.php?p=else

导致错误的网址实际上是服务器无法找到的,例如:

http://mysite.com/Idontexist.php
http://mysite.com/nosuchfolder

有谁知道如何防止这种情况?

1 个答案:

答案 0 :(得分:2)

你必须添加一个RewriteCond来检查Mod_Rewrite是否已经进行了内部重定向,L falg只停止当前运行,然后如果更改了url,则会重新执行.htaccess指令(这是这种情况):

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\.php$
RewriteRule ^([^/]+)/([^/]+)/*$ /$1.php?p=$2 [L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\.php$
RewriteRule ^([^/]+)/*$ /$1.php [L]
</IfModule>