使用mod_rewrite的Apache内部重定向无限循环

时间:2018-07-11 02:55:58

标签: regex apache .htaccess

好的,即使已经为类似问题提供了所有解决方案,这似乎也无法解决。

使用以下代码拥有.htaccess文件:

RewriteEngine on

RewriteBase /amit/public/

RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]

RewriteCond ${REQUEST_FILENAME} !-f
RewriteCond ${REQUEST_FILENAME} !-d
RewriteRule ^(.*[^/])/?$ $1.php [NC,L]

没有RewriteCond %{ENV:REDIRECT_STATUS} 200和条件后的规则,服务器将经历无限循环。 apache日志文件如下所示:

r->uri = /amit/public/activities.php.php.php.php.php.php.php.php
redirected from r->uri = /amit/public/activities.php.php.php.php.php.php.php
redirected from r->uri = /amit/public/activities.php.php.php.php.php.php
redirected from r->uri = /amit/public/activities.php.php.php.php.php
redirected from r->uri = /amit/public/activities.php.php.php.php
redirected from r->uri = /amit/public/activities.php.php.php
redirected from r->uri = /amit/public/activities.php.php
redirected from r->uri = /amit/public/activities.php
redirected from r->uri = /amit/public/activities

从本质上讲,它在评估为request_path.php时不会打开文件,但会继续添加更多.php

如果我确实添加了这种情况,它将完全不添加.php并在日志中的这一行返回404错误:

File does not exist: /Applications/MAMP/htdocs/amit/public/activities

被禁闭24小时试图解决它,没有运气...

1 个答案:

答案 0 :(得分:1)

您在$中使用%而不是%{REQUEST_FILENAME}

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*[^/])/?$ $1.php [NC,L]

当文件不存在时,仍然会导致重定向循环。您可以检查是否也添加了扩展名:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*[^/])(?<!\.php)/?$ $1.php [NC,L]