【问题标题】:Rewrite rules cause infinite redirect loop重写规则导致无限重定向循环
【发布时间】:2015-06-13 19:45:39
【问题描述】:

我的文件夹结构如下:

/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]

本质上,我想将所有 URL 重写到 /site/ 目录,并让现有文件简单地重写到 site 文件夹中的路径,同时将任何不存在的文件重写到 /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 文件中,则可能会再次运行它们,但我不确定为什么或如何防止它。我尝试在第二个RewriteRule 之前添加RewriteCond 以仅在文件名不是/site/index.php 时运行规则,但这会导致所有URL(甚至现有文件)重写为/site/index。

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

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

【问题讨论】:

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


    【解决方案1】:

    因为RewriteCond,第一个RewriteRule 永远不会被使用。它总是假的。 %{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]
    

    【讨论】:

    • @AngeloGeels 我对答案也是如此:)
    • 很高兴为您提供帮助。祝你好运
    猜你喜欢
    • 2013-06-25
    • 2019-09-16
    • 2015-02-21
    • 1970-01-01
    • 2017-08-16
    • 2013-12-12
    • 1970-01-01
    • 2012-03-05
    • 1970-01-01
    相关资源
    最近更新 更多