HTTP 到 HTTPS 重写规则不起作用

raj*_*eev 3 apache2 mod-rewrite mod-ssl

Ubuntu 14.04

Apache/2.4.7

我在这里发布我的虚拟主机和默认 ssl 主机的 conf 文件。无法弄清楚我做错了什么。

http://<website_url> 显示文件夹的索引。我想将此重定向到https。

https://<website_url> 打开很好。

重要提示:我尚未启用默认 SSL 站点。

 cat default-ssl.conf|grep -v "#"

<IfModule mod_ssl.c>
      <VirtualHost _default_:443>
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        SSLEngine on
        SSLCertificateFile  /etc/ssl/certs/ssl-cert-snakeoil.pem
        SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key

        <FilesMatch "\.(cgi|shtml|phtml|php)$">
                SSLOptions +StdEnvVars
        </FilesMatch>
        <Directory /usr/lib/cgi-bin>
                SSLOptions +StdEnvVars
        </Directory>

        BrowserMatch "MSIE [2-6]" \
                nokeepalive ssl-unclean-shutdown \
                downgrade-1.0 force-response-1.0
        BrowserMatch "MSIE [17-9]" ssl-unclean-shutdown

        RewriteEngine On
        RewriteCond %{HTTPS} off
        RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
    </VirtualHost>
</IfModule>
Run Code Online (Sandbox Code Playgroud)

这是 mywebsite 配置文件:

cat www.mywebsite.com.conf|grep -v "#"

<VirtualHost *:443>
    ServerName www.mywebsite.com:443
    ServerAlias www.mywebsite.com
    ServerAdmin abc@mywebsite.com
    DocumentRoot /var/www/www.mywebsite.com/html

    Header always set Strict-Transport-Security "max-age=63072000; includeSubDomains"
     <IfModule mod_rewrite.c>
      RewriteEngine On
      RewriteCond %{HTTPS} off
      RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
    </IfModule>

SSLEngine on   
    SSLEngine on
        SSLCertificateFile /etc/apache2/ssl/apache.crt
        SSLCertificateKeyFile /etc/apache2/ssl/apache.key

    <FilesMatch "\.(cgi|shtml|phtml|php)$">
        SSLOptions +StdEnvVars
    </FilesMatch>
    <Directory /usr/lib/cgi-bin>
        SSLOptions +StdEnvVars
    </Directory>

    BrowserMatch "MSIE [2-6]" \
        nokeepalive ssl-unclean-shutdown \
        downgrade-1.0 force-response-1.0
    BrowserMatch "MSIE [17-9]" ssl-unclean-shutdown

    ErrorLog ${APACHE_LOG_DIR}/ssl.error.log
    CustomLog ${APACHE_LOG_DIR}/ssl.access.log combined
</VirtualHost>
Run Code Online (Sandbox Code Playgroud)

Let*_*ety 8

如果您希望http://www.mywebsite.com/始终发送过去,https您应该使用,redirect因为使用mod_rewrite不是推荐的行为。

根据重定向请求到 SSL Apache wiki 页面:

使用 SSL 时,您经常会至少有两个虚拟主机:一个在端口 80 上为普通请求提供服务,另一个在端口 443 上为 SSL 提供服务。如果您希望将用户从非安全站点重定向到 SSL 站点,您可以在非安全 VirtualHost 中使用普通的 Redirect 指令

因此,尝试在您的非安全 VirtualHost 中添加此指令:

Redirect permanent / https://www.mywebsite.com/
Run Code Online (Sandbox Code Playgroud)

如果你无论如何都想使用rewrite规则,你应该在 non-secure 中添加这些行VirtualHost

RewriteEngine On
# This will enable the Rewrite capabilities

RewriteCond %{HTTPS} !=on
# This checks to make sure the connection is not already HTTPS

RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
# This rule will redirect users from their original location, to the same location but using HTTPS.
# i.e.  http://www.mywebsite.com/foo/ to https://www.mywebsite.com/foo/
Run Code Online (Sandbox Code Playgroud)

HTTP to HTTPS Apache wiki 页面中所述。


您的配置不起作用,因为它没有定义处理 http 请求并将它们重定向到安全 VirtualHost 的非安全 VirtualHost(通常在端口 80 上)。

尝试添加这些行:

<VirtualHost *:80>
   ServerName dev.dom1.com
   Redirect permanent / https://dev.dom1.com/
</VirtualHost>
Run Code Online (Sandbox Code Playgroud)

在这种情况下,您不需要 aDocumentRoot因为这VirtualHost是重定向所有内容。

Rewrite配置文件中显示的规则保护安全VirtualHost不被http协议访问,例如http://www.mywebsite.com:443/将是https://www.mywebsite.com:443/

您还应该检查您的网站是否从 HTML 页面中链接到了正确的页面 (https)。