用于将域重定向到子文件夹的Apache RewriteRule导致重定向循环

时间:2012-03-09 02:55:10

标签: apache mod-rewrite

我几乎已经用mod_rewrite了。我已经取出了每个其他重写语句(RewriteEngine on除外...虽然我应该因为这显然是问题)但这仍然不起作用。我已经包含了我的整个虚拟主机文件,所以希望有人可以告诉我这里有什么问题。我想键入以下内容并重定向(内部即地址栏中不可见),如此

  1. www.example.com ---> www.example/files/main/index.php
  2. www.business.example.com ---> www.example/files/business/index.php
  3. www.tech.example.com ---> www.example/files/tech/index.php
  4. www.sports.example.com ---> www.example/files/sports/index.php
  5. ...
  6. 我尝试同时处理1和2-5 +,这是一场灾难。所以我暂时放弃2-5 +,只是试着让1工作,不管我做什么,我得到一个重定向循环。显然,我更喜欢复制粘贴的完美答案,但我甚至会和某人解释为什么mod_rewrite坚持赞成重定向循环。谢谢。

    <VirtualHost *:80>
    RewriteEngine on
    RewriteRule ^$ /files/main
    
    ServerAdmin www-data@example.com
    ServerName www.example.com
    ServerAlias example.com *.example.com
    DocumentRoot /var/www
    DirectoryIndex index.html index.php filename.html index.cgi index.pl default.htm
    
    <Directory /var/www>
    suPHP_Engine on
    suPHP_AddHandler application/x-httpd-php .php
    suPHP_ConfigPath /etc/php5/cgi/
    AllowOverride None
    Options -Indexes +FollowSymLinks
    </Directory>
    
    LogLevel warn 
    LogFormat "%v %l %u %t \"%r\" %>s %b" comonvhost
    CustomLog /var/log/apache2/access.log comonvhost
    </VirtualHost>
    

1 个答案:

答案 0 :(得分:0)

在mod_rewrite文档中:

http://httpd.apache.org/docs/current/rewrite/remapping.html#canonicalhost

如果你厌倦了mod_rewrite,那可能就是这样。 (你想明确地将这些网站重定向到新网址,还是无形地重写?)

我无法重现您遇到的问题。我的设置有很多VirtualHost条目和目录条目,正在进行相当多的重写。

我在我的主VirtualHost中添加了一条与你的规则非常相似的规则。现在您应该知道这没有任何匹配,因为对文档根目录的请求是/而不是空字符串。

RewriteRule ^$ /foo/bar

访问网站时我没有获得重定向循环;它完全被忽略了。然后我改为:

RewriteRule ^/$ /foo/bar

重新启动服务器,转到它并获得旧的在此服务器上找不到请求的URL / var / www / foo / bar。没有循环。

您的设置中还有其他内容。也许你正在重写的URL有一个触发循环的本地.htaccess。 (但是如何通过^$的匹配来重写它们。)

相关问题