AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 550204
Accepted
rajeev
rajeev
Asked: 2014-11-16 22:30:55 +0800 CST2014-11-16 22:30:55 +0800 CST 2014-11-16 22:30:55 +0800 CST

HTTP 到 HTTPS 重写规则不起作用

  • 772

Ubuntu 14.04

阿帕奇/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>

这是我的网站配置文件:

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

<VirtualHost *:443>
    ServerName www.mywebsite.com:443
    ServerAlias www.mywebsite.com
    ServerAdmin [email protected]
    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>
apache2
  • 2 2 个回答
  • 45161 Views

2 个回答

  • Voted
  1. Best Answer
    Lety
    2014-11-17T08:13:31+08:002014-11-17T08:13:31+08:00

    如果你想要它http://www.mywebsite.com/总是被发送过来https你应该使用redirect因为使用mod_rewrite不是推荐的行为。

    根据Redirect Request to SSL Apache 维基页面:

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

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

    Redirect permanent / https://www.mywebsite.com/
    

    如果你想使用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/
    

    如HTTP 到 HTTPS Apache 维基页面中所述。


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

    尝试添加这些行:

    <VirtualHost *:80>
       ServerName dev.dom1.com
       Redirect permanent / https://dev.dom1.com/
    </VirtualHost>
    

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

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

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

    • 8
  2. Paul Bauer
    2015-05-25T22:34:50+08:002015-05-25T22:34:50+08:00

    这是一篇旧文章,但在 Ubuntu 14.04 中,原来的重写是有效的,你只需要把它改成:

    <Directory /var/www/>
    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
    </Directory>
    
    • 0

相关问题

  • 如何使用 fastcgi 和简单的测试脚本设置 apache?

  • 新贵监督的 Apache 初始化脚本?

  • http://localhost/ 不工作

  • 访问启用的虚拟主机时出现 403 禁止错误

  • 如何设置一台机器来将我的网站托管到世界各地 - 使用我自己的网址?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve