【问题标题】:Apache permanent redirect goes to www automaticallyApache 永久重定向自动转到 www
【发布时间】:2017-10-18 14:22:30
【问题描述】:

我的域名是 example.com,没有 www。因此,如果我输入 www.example.com 则它不起作用,但 example.com 有效。所以我这样配置apache

<VirtualHost *:80>
    ServerName example.com
    ServerAdmin webmaster@example.com
    Redirect permanent / https://example.com/
</VirtualHost>

<VirtualHost *:443>
    ServerName example.com
    ServerAdmin webmaster@example.com
    DocumentRoot path/to/project/public
    SSLEngine on
    SSLCertificateFile /path/to/keys/xxx.crt
    SSLCertificateKeyFile /path/to/keys/xxx.key
    ErrorLog /var/log/apache2/error_log
    CustomLog /var/log/apache2/access_log combined
    <Directory "path/to/project/public">
        Options FollowSymLinks
        Require all granted
    </Directory>
</VirtualHost>

现在,如您所见,我将永久重定向到 https,例如

Redirect permanent / https://example.com/

但是这个重定向默认会加上域名的www。所以重定向的 url 变成了https://www.example.com/。显然,我的网站无法通过 www 访问,因为它是在没有 www 的情况下注册的。所以请告诉我如何使重定向工作并在没有 https 的情况下转到 https://example.com/

【问题讨论】:

    标签: apache redirect


    【解决方案1】:

    添加别名

    <VirtualHost *:443>
      ServerName example.com
      ServerAlias www.example.com
    </VirtualHost>
    

    这要求 www.yourdomain.com 指向与 yourdomain.com 相同的位置。但是 www 可能不适用于您的 SSL 证书,这取决于证书的特殊性。

    我通常允许在我的网站上同时使用这两个,因为有些人坚持在输入地址时包含 www。

    Apache 的文档可以提供更多细节https://httpd.apache.org/docs/2.2/vhosts/name-based.html

    就您遇到的重定向问题而言: 确保您没有一些 RewriteEngine 规则将您的非 www 请求重写为 www。您的站点目录中可能有一个 .htaccess 文件正在执行重写/重定向。

    它可能看起来像:

    RewriteEngine On 
    RewriteCond %{HTTPS} !=on 
    RewriteRule ^/?(.*) https://www.%{SERVER_NAME}/$1 [R,L]
    

    这意味着您应该删除最后一个重写规则中的 www

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-29
      • 1970-01-01
      • 2021-11-10
      • 2016-02-26
      • 2016-09-06
      • 2011-01-17
      相关资源
      最近更新 更多