【发布时间】: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