【问题标题】:Redirect a website from http to https将网站从 http 重定向到 https
【发布时间】:2013-07-03 06:03:10
【问题描述】:

我目前正在运行一个用 struts 编写的网站。我正在使用 Apache Web 服务器并使用 AJP 将我的 Web 应用程序代理传递给 Apache Tomcat 服务器。

我的服务器是托管在亚马逊的 Ubuntu 服务器。我能够使用 http 成功运行我的 Web 应用程序。

现在我计划为上述 Web 应用程序实施 SSL。我已完成所有 SSL 实施步骤,现在我可以使用 https 成功访问我的 Web 应用程序,即 https://{MY_APPLICATION_URL}

现在的问题是:当用户输入我的应用程序 URL(如 http://{MY_APPLICATION_URL} 或 {MY_APPLICATION_URL})时,我需要重定向并使其仅使用 https://{MY_APPLICATION_URL}。不能有 http 访问。

我找到了以下链接:http://www.sslshopper.com/apache-redirect-http-to-https.html在 apache 服务器中安装了重写模块(通过 webmin)。然后在我的应用程序的虚拟主机中添加以下行(编辑指令)。

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

即使经过上述重定向,当用户输入 http://{MY_APPLICATION_URL} 或 {MY_APPLICATION_URL} 时,它也会显示我的 apache 服务器的默认欢迎页面。但是,当我输入 https://{MY_APPLICATION_URL} 时,我能够成功访问我的网络应用程序。

谁能指导我哪里做错了。

谢谢。

【问题讨论】:

    标签: apache redirect http-redirect


    【解决方案1】:

    这个解决方案帮助了我。

    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteCond %{SERVER_PORT} 80
        RewriteRule ^(.*)$ https://domain.name/$1 [R,L]
    </IfModule>
    

    希望对你也有帮助。

    【讨论】:

    • no..它对我不起作用:(你是否在 apache 中启用了 mod_rewrite 模块或 rewrite 模块?
    • 是的,在我的 httpd.conf (CentOS) 我有类似 LoadModule rewrite_module modules/mod_rewrite.so 的行;您是否还为您的域配置了 VirtualHost 以监听 443 端口?
    • 是的。我已经将我的 VirtualHost 配置为监听 443 端口。因此,我能够成功访问 https://{MY_APPLICATION_URL}。现在问题仅在于 http://{MY_APPLICATION_URL} 访问。
    • 那么 .htaccess 呢?它有效吗?我的意思是它是否已处理。我也想知道我的例子会发生什么?您是否得到相同的反应(重定向到 apache 示例页面)?
    【解决方案2】:

    在服务器版本上与我合作:Apache/2.4.6 (CentOS)

    将其添加到文件 /etc/httpd/conf/httpd.conf

    LoadModule rewrite_module modules/mod_rewrite.so
    
    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteCond %{SERVER_PORT} 80
        RewriteRule ^(.*)$ https://%{HTTP_HOST}$1 [R,L]
    </IfModule>
    

    或者使用php

    <?php
    function redirectTohttps() {
    if($SERVER['HTTPS']!=”on”) {
    $redirect= “https://”.$SERVER['HTTPHOST'].$SERVER['REQUEST_URI'];
    header(“Location:$redirect”); } }
    ?>
    

    【讨论】:

      【解决方案3】:

      您可以轻松做到:

      • Webmin > 配置 Webmin > Ssl 配置

      • 在 > 将非 SSL 请求重定向到 SSL 模式?

      • 重新加载浏览器并清除缓存(Ctrl + Shift +R)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-01-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多