2013-08-25 119 views
1

我想设置我的网站只使用www,非www应永久重定向到www。 This answer建议使用两个虚拟主机,但它会导致我进入重定向循环。Apache2配置导致重定向循环

下面是该网站的配置:

<VirtualHost *:80> 
    ServerName www.mydomain.com 

    DirectoryIndex index.html index.php 
    DocumentRoot /home/me/sites/mydomain.com/htdocs 

    # Log file locations 
    LogLevel warn 
    ErrorLog /home/me/sites/mydomain.com/logs/error.log 
    CustomLog /home/me/sites/mydomain.com/logs/access.log combined 
</VirtualHost> 

<VirtualHost *:80> 
    ServerName mydomain.com 
    Redirect permanent/http://www.mydomain.com/ 
</VirtualHost> 

当我访问它成功地重定向到www的网站的非www版本,但Chrome浏览器,然后告诉我,有一个重定向循环。

起初我以为它可能是.htaccess在我的文档根目录中,但删除该文件后它仍然发生。这只是一个简单的WordPress的网站。

任何人都可以看到会导致这种情况发生的配置错误吗?如果不是,我怎样才能缩小原因?

回答

1

对于非www,您不需要单独的VirtualHost条目,而是使用ServerAlias。也重定向到www只是使用这样的规则:

<VirtualHost *:80> 
    ServerName www.mydomain.com 
    ServerAlias mydomain.com 
    DirectoryIndex index.html index.php 
    DocumentRoot /home/me/sites/mydomain.com/htdocs 

    # Log file locations 
    LogLevel warn 
    ErrorLog /home/me/sites/mydomain.com/logs/error.log 
    CustomLog /home/me/sites/mydomain.com/logs/access.log combined 

    RewriteEngine On 
    RewriteCond %{HTTP_HOST} !^www\. [NC] 
    RewriteRule^http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L] 

</VirtualHost> 
相关问题