2013-08-25 121 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> 
相關問題