重写规则会导致无限重定向循环

时间:2015-06-13 19:45:39

标签: apache .htaccess mod-rewrite url-rewriting

我的文件夹结构如下所示:

/var/www/.htaccess
/var/www/site/index.php
/var/www/site/images/test.png

.htaccess文件的位置如下:

RewriteEngine On
RewriteCond /site/%{REQUEST_FILENAME} -f
RewriteRule ^(.*)$ /site/%{REQUEST_FILENAME} [L]
RewriteRule ^(.*)$ /site/index.php [L,QSA]

基本上,我想重写/ site /目录的所有URL,并让现有文件只重写到site文件夹中的路径,同时使任何不存在的文件重写为{{1}而不是。

上面的/site/index.php文件适用于.htaccess,但对于/images/test.png,它会导致无限循环,如Apache错误日志中所述:

/

根据文档,如果出于各种原因,重写规则可能会再次运行[Sat Jun 13 21:42:04.003016 2015] [core:error] [pid 12949] [client 127.0.0.1:50560] AH00124: 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. [Sat Jun 13 21:42:04.003020 2015] [core:debug] [pid 12949] core.c(3533): [client 127.0.0.1:50560] AH00121: r->uri = /site/index.php [Sat Jun 13 21:42:04.003022 2015] [core:debug] [pid 12949] core.c(3540): [client 127.0.0.1:50560] AH00122: redirected from r->uri = /site/index.php [Sat Jun 13 21:42:04.003033 2015] [core:debug] [pid 12949] core.c(3540): [client 127.0.0.1:50560] AH00122: redirected from r->uri = /site/index.php [Sat Jun 13 21:42:04.003035 2015] [core:debug] [pid 12949] core.c(3540): [client 127.0.0.1:50560] AH00122: redirected from r->uri = /site/index.php [Sat Jun 13 21:42:04.003037 2015] [core:debug] [pid 12949] core.c(3540): [client 127.0.0.1:50560] AH00122: redirected from r->uri = /site/index.php [Sat Jun 13 21:42:04.003038 2015] [core:debug] [pid 12949] core.c(3540): [client 127.0.0.1:50560] AH00122: redirected from r->uri = /site/index.php [Sat Jun 13 21:42:04.003040 2015] [core:debug] [pid 12949] core.c(3540): [client 127.0.0.1:50560] AH00122: redirected from r->uri = /site/index.php [Sat Jun 13 21:42:04.003041 2015] [core:debug] [pid 12949] core.c(3540): [client 127.0.0.1:50560] AH00122: redirected from r->uri = /site/index.php [Sat Jun 13 21:42:04.003048 2015] [core:debug] [pid 12949] core.c(3540): [client 127.0.0.1:50560] AH00122: redirected from r->uri = /site/index.php [Sat Jun 13 21:42:04.003049 2015] [core:debug] [pid 12949] core.c(3540): [client 127.0.0.1:50560] AH00122: redirected from r->uri = /site/index.php [Sat Jun 13 21:42:04.003050 2015] [core:debug] [pid 12949] core.c(3540): [client 127.0.0.1:50560] AH00122: redirected from r->uri = / 个文件,但我不确定原因,或者如何防范它。我尝试在第二个.htaccess之前添加RewriteCond以仅运行规则,如果文件名已经不是/site/index.php,但这会导致所有网址(甚至是现有文件)重写到/site/index.php。

为了更清楚我想要什么,我希望将以下URI重写为以下路径:

  • RewriteRule - > /
  • /site/index.php - > /test
  • /site/index.php - > /images/test.png(因为文件存在)
  • /site/images/test.png - > /images/bla.png(因为该文件不存在)

1 个答案:

答案 0 :(得分:0)

由于RewriteRule,永远不会使用第一个RewriteCond。它总是错误的。 %{REQUEST_FILENAME}返回服务器上的完整路径,没有文件名。

仅第二条规则就会使无限循环将/site/index.php重定向到自身。

如果您解释了自己想要的内容,我们会尽力帮助您完成htaccess的工作

更新

RewriteEngine on

# if file exist only add /site/
RewriteCond %{DOCUMENT_ROOT}/site/%{REQUEST_URI} -f
RewriteRule ^(.+)$ /site/$1 [L]

# if no /site/ in request rewrite to index.php
RewriteCond %{REQUEST_URI} !^/site/
RewriteRule ^ /site/index.php [L]