【问题标题】:Apache2 configuration causing redirect loopApache2 配置导致重定向循环
【发布时间】:2013-08-28 00:53:57
【问题描述】:

我正在尝试将我的网站设置为仅使用 www,而非 www 应永久重定向到 www。 This answer 建议使用两个虚拟主机,但这会导致我进入重定向循环。

这是网站的配置:

<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 网站。

任何人都可以看到导致这种情况发生的配置错误吗?如果没有,我该如何缩小原因?

【问题讨论】:

    标签: wordpress apache redirect apache2


    【解决方案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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-04
      • 2014-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多