2015-06-13 102 views
0

我的文件夹结构如下所示:重写规则导致无限重定向循环

/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/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.php。

要论正是我想要的,我想下面的URI改写为以下路径更加清晰:

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

回答

0

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

单独的第二个规则使无限循环重定向/site/index.php本身。

如果你解释你想要什么,我们尽力帮助youto使工作htaccess的

UPDATE

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] 
+0

我已经更新的问题更清楚一点。 – Codecat

+0

@AngeloGeels我也是这样回答:) – splash58

+0

谢谢,这个解决了! – Codecat