【问题标题】:apache redirect HTTPS to canonical HTTPSapache 将 HTTPS 重定向到规范的 HTTPS
【发布时间】:2018-08-03 00:03:28
【问题描述】:

我希望对我网站的所有访问都强制使用 HTTPS (https://support.google.com/webmasters/answer/6073543?hl=en)。

我还想强制规范 www URL 访问 (https://www.yes-www.org/why-use-www/)

我正在尝试根据 Apache 建议使用重定向指令 https://wiki.apache.org/httpd/RedirectSSLhttps://httpd.apache.org/docs/2.4/rewrite/remapping.html#canonicalhost 这样做

我有一个有效的 Lets-encrypt 证书,其中包含 www 和裸域。

我已配置 *:80 和 *:443 VirtualHost 重定向。 /etc/httpd/conf.d/www.example.com.conf:

<VirtualHost *:80>
  ServerName  www.example.com
  ServerAlias     example.com
  Redirect permanent / https://www.example.com
</VirtualHost>

<VirtualHost *:443>
  ServerName  example.com
  SSLEngine on
  SSLCertificateFile    /etc/letsencrypt/live/www.example.com/cert.pem
  SSLCertificateKeyFile /etc/letsencrypt/live/www.example.com/privkey.pem
  SSLCACertificateFile  /etc/letsencrypt/live/www.example.com/fullchain.pem
  Redirect permanent / https://www.example.com
</VirtualHost>

<VirtualHost *:443>
  ServerName www.example.com
  SSLEngine on
  SSLCertificateFile    /etc/letsencrypt/live/www.example.com/cert.pem
  SSLCertificateKeyFile /etc/letsencrypt/live/www.example.com/privkey.pem
  SSLCACertificateFile  /etc/letsencrypt/live/www.example.com/fullchain.pem
  DocumentRoot "/var/www/html/www.example.com"
</VirtualHost>

<Directory "/var/www/html/www.example.com">
  Order allow,deny
  Allow from all
</Directory>

如果我指定基本 URL(example.com、www.example.com、https://example.com 等),一切正常。但是,如果我在裸 HTTPS 请求上指定一个页面,则重定向会吃掉根斜线(https://example.com/index.html 变为 https://www.example.comindex.html)。

【问题讨论】:

    标签: apache lets-encrypt


    【解决方案1】:

    我对所有非 ssl 到 ssl 执行以下操作 -

    <VirtualHost *:80>
      ServerName example.org
      ServerAlias www.example.org
      RewriteEngine on
      RewriteRule ^/(.*)$ https://www.example.org/$1 [R,L]
    </VirtualHost>
    

    对于https://example.org 略有不同应该做同样的事情,只重定向到www.example.org

    <VirtualHost your.ip.add.ress:443>
      ServerName example.org
      RewriteEngine on
      RewriteRule ^/(.*)$ https://www.example.org/$1 [R,L]
    
      *snip*  
      Normal SSL certificate/key stuff goes here
      *snip*
    
    </VirtualHost>
    

    【讨论】:

    • 我不反对 Rewrite,但 Apache 文档明确推荐 Redirect。请参阅 OP 中的链接。
    【解决方案2】:

    RedirectMatch 似乎解决了类似于 ivanivan 建议的 Rewrite 的问题。将 *:443 VHost 部分中的重定向行更改为以下内容似乎可以解决问题:

    RedirectMatch permanent ^/?(.*) https://www.example.com/$1
    

    我仍然不明白为什么简单的重定向不适用于 HTTPS。

    顺便说一句,https://salferrarello.com/chrome-clear-redirect-cache/ 在测试期间禁用 Chrome 中的重定向缓存很有用。

    【讨论】:

      猜你喜欢
      • 2011-08-18
      • 2019-11-08
      • 2017-09-28
      • 2013-08-20
      • 1970-01-01
      • 1970-01-01
      • 2016-03-06
      • 1970-01-01
      • 2019-12-17
      相关资源
      最近更新 更多