如何在 Apache 上将 HTTP 重定向到 HTTPS? [英] How to Redirect HTTP to HTTPS on Apache?

查看:81
本文介绍了如何在 Apache 上将 HTTP 重定向到 HTTPS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

环境:Ubuntu 和 Apache.

Environment: Ubuntu with Apache.

尝试设置从 http 到 https 的自动重定向.

Trying to setup automatic redirection from http to https.

我试过了:

<VirtualHost *:80>
    RewriteEngine On
    RewriteCond %{HTTPS} !=on
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
</VirtualHost>

<VirtualHost *:443>
    SSLEngine on
    SSLCertificateFile    <path to your crt file>
    SSLCertificateKeyFile   <path to your private key file>

    # ...
</VirtualHost>

    RewriteEngine on
    ReWriteCond %{SERVER_PORT} !^443$
    RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R,L]

从 mydomain.com --- 到 --->(https://) mydomain.com

From mydomain.com --- To ---> (https://) mydomain.com

有什么想法吗?

推荐答案

确保您可以通过 HTTP 和 HTTPS 访问您的网站.还要确保启用了 mod_rewrite,然后您可以将这些行添加到您的 .htaccess 文件中.

Make sure you can access your website both through HTTP and HTTPS. Also make sure mod_rewrite is enabled, then you can add these lines to your .htaccess file.

RewriteEngine On 
RewriteCond %{HTTPS} off 
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

这篇关于如何在 Apache 上将 HTTP 重定向到 HTTPS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆