【问题标题】:HTTP + HTTPS + www + non-www Apache configHTTP + HTTPS + www + 非 www Apache 配置
【发布时间】:2018-11-15 11:21:09
【问题描述】:

在我的 Apache 配置中,所有内容都重定向到 HTTPS(这很好)。但是https://www.example.comhttps://example.com 仍然存在。

问题:如何只有https://www.example.com而不是非www?

我应该使用301 Redirection 还是其他技术?

这样的配置应该怎么改:

<VirtualHost *:80>
  ServerName example.com
  ServerAlias *.example.com
  RewriteEngine on
  RewriteCond %{HTTPS} !on
  RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
</VirtualHost>

<IfModule mod_ssl.c>
<VirtualHost *:443>
  ServerName example.com
  ServerAlias *.example.com
  DocumentRoot /home/www/example
  <Directory />
    Options FollowSymLinks
    AllowOverride All
    Order deny,allow
    Allow from all
    Require all granted
  </Directory>
  SSLCertificateFile /etc/letsencrypt/live/example.com/cert.pem
  SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
  Include /etc/letsencrypt/options-ssl-apache.conf
  SSLCertificateChainFile /etc/letsencrypt/live/example.com/chain.pem
</VirtualHost>
</IfModule>

?

【问题讨论】:

    标签: apache http https virtualhost no-www


    【解决方案1】:

    Relymcd's answer 解决了这个问题,但它还需要证书行存在(如果没有它会失败):

    <VirtualHost *:443>
        ServerName example.com
        Redirect 301 / https://www.example.com/
        SSLCertificateFile /etc/letsencrypt/live/example.com/cert.pem
        SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
        Include /etc/letsencrypt/options-ssl-apache.conf
        SSLCertificateChainFile /etc/letsencrypt/live/example.com/chain.pem
    </VirtualHost>
    

    【讨论】:

      【解决方案2】:

      一种方法是更改​​当前虚拟主机ServerName www.example.com,并为非www添加一个新的虚拟主机

      <VirtualHost *:443> ServerName example.com Redirect 301 / https://www.example.com/ </VirtualHost>

      【讨论】:

      • 是的,但这会破坏当前链接:例如:https://example.com/article1 将被重定向到 https://www.example.com/,还是我错了?
      • @basj 不,我相信该路径是保留的。
      • 另外,它不起作用,缺少一个参数:Redirect 301 &lt;argumentmissinghere&gt; https://www.example.com/
      • 我已经更新了答案 - 我错过了 / ... 也确认了正在维护的路径 https://example.com/about 将重定向到 https://www.example.com/about - 确保你有尾部斜杠,否则重定向转到https://www.example.comabout
      猜你喜欢
      • 2023-03-16
      • 2017-12-04
      • 1970-01-01
      • 2014-12-16
      • 2015-04-06
      • 2014-12-13
      • 1970-01-01
      • 2018-09-26
      • 2012-02-14
      相关资源
      最近更新 更多