【问题标题】:HTTP to HTTPS in Apache for a specific virtual host特定虚拟主机的 Apache 中的 HTTP 到 HTTPS
【发布时间】:2017-12-01 01:19:21
【问题描述】:

我在 Apache 上有虚拟主机,其配置类似于以下内容:

<VirtualHost *:80>    
    DocumentRoot "/home/doe/www/factory/public"
    <Directory "/home/doe/www/factory/public">
        AllowOverride All
        Options Indexes FollowSymLinks
        Require all granted
    </Directory>
    ServerName doe.example.com
    ServerAlias qa.dev
    ServerAlias doe202.ddns.net
    ErrorLog "/home/doe/logs/doe.log"
    CustomLog "/home/doe/logs/aq-access.log" common    
</VirtualHost>
<VirtualHost *:443>   
    DocumentRoot "/home/doe/www/factory/public"
    <Directory "/home/doe/www/factory/public">
        AllowOverride All
        Options Indexes FollowSymLinks
        Require all granted
    </Directory>
    ServerName doe.example.com
    ServerAlias qa.dev
    ServerAlias doe202.dddns.net
    ErrorLog "/home/doe/logs/doe.log"
    CustomLog "/home/doe/logs/aq-access.log" common  
    SSLEngine on        
      SSLCertificateFile  /etc/letsencrypt/live/doe.example.com/fullchain.pem
      SSLCertificateKeyFile  /etc/letsencrypt/live/doe.exmple.com/privkey.pem
</VirtualHost>

我需要实现的是http://doe.example.com 重定向到https://doe.example.com,当然要附加任何URI。

我已尝试查找该问题的this answerhttp to https apache redirection,但它会将所有主机重定向到 HTTPS。

我还尝试更改端口 80 虚拟主机中的 serverName 指令,但这将完全阻止使用 http 的用户访问。

【问题讨论】:

  • 把重写放在
  • @ShingLam 注意VirtualHost *:80 包含serverAlias,它会给出相同的结果。即所有主机将被重定向到 https。
  • 错过了服务器别名。为什么不为所有其他服务器别名设置另一个 VirtualHost *;80

标签: apache .htaccess ssl redirect


【解决方案1】:

我发现一个解决方案取决于用于重定向 www to non www with hard redirect 的解决方案,如下所示:

RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteCond %{HTTP_HOST} ^doe\.example\.com$ [NC]
    RewriteRule ^(.*)$ https://doe.example.com/$1 [R=301,L]
# ... the rest of .htaccess

这样,只有http://doe.example.com 将被重定向到https://doe.example.com,而其他别名将按照请求保持其协议。

顺便说一下,网站是项目,项目的所有.htaccess配置都在上面的配置旁边。

【讨论】:

    猜你喜欢
    • 2016-07-16
    • 2014-04-11
    • 2018-03-23
    • 2016-02-26
    • 2018-10-12
    • 2017-04-08
    • 2017-06-04
    • 2018-12-06
    • 2020-05-08
    相关资源
    最近更新 更多