如何在 Apache 服务器上自动将 HTTP 重定向到 HTTPS?

问题描述 投票:0回答:13

环境 Centos 与 apache

我正在尝试设置从 http 到 https 的自动重定向

From manage.mydomain.com --- To ---> https://manage.mydomain.com

我尝试将以下内容添加到我的 httpd.conf 文件中,但它不起作用:

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

我该如何修复它?

linux apache .htaccess webserver httpd.conf
13个回答
301
投票

我实际上遵循了这个例子,它对我有用:)

NameVirtualHost *:80
<VirtualHost *:80>
   ServerName mysite.example.com
   Redirect permanent / https://mysite.example.com/
</VirtualHost>

<VirtualHost _default_:443>
   ServerName mysite.example.com
  DocumentRoot /usr/local/apache2/htdocs
  SSLEngine On
 # etc...
</VirtualHost>

然后做:

/etc/init.d/httpd restart


215
投票
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI}

http://www.sslshopper.com/apache-redirect-http-to-https.html

http://www.cyberciti.biz/tips/howto-apache-force-https-secure-connections.html


138
投票

搜索

apache redirect http to https
并登陆这里。这就是我在 ubuntu 上所做的:

1) 启用模块

sudo a2enmod rewrite
sudo a2enmod ssl

2) 编辑您的站点配置

编辑文件

/etc/apache2/sites-available/000-default.conf

内容应为:

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

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

    # Rest of your site config
    # ...
</VirtualHost>
  • 请注意,SSL 模块需要证书。您需要指定现有证书(如果您购买了证书)或自行生成自签名证书

3)重新启动apache2

sudo service apache2 restart

17
投票

不推荐使用 mod_rewrite,而是使用虚拟主机和重定向。

如果您倾向于使用 mod_rewrite:

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.example.com/foo/ to https://www.example.com/foo/
# The leading slash is made optional so that this will work either in
# httpd.conf or .htaccess context

参考:Httpd Wiki - RewriteHTTPToHTTPS

如果您正在寻找 301 永久重定向,则重定向标志应为,

 R=301

所以 RewriteRule 会是这样的,

RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R=301,L]

14
投票

我需要这个,就像将所有

http
流量从我的服务器上的默认 apache 主页重定向到通过
https
提供的服务一样简单。

由于在配置 apache 时我仍然很green,所以我更喜欢避免直接使用

mod_rewrite
,而是选择像这样更简单的东西:

<VirtualHost *:80>
  <Location "/">
     Redirect permanent "https://%{HTTP_HOST}%{REQUEST_URI}"
  </Location>
</VirtualHost>

<VirtualHost *:443>
  DocumentRoot "/var/www/html"
  SSLEngine on
  ...
</VirtualHost>

我喜欢这个,因为它允许我使用 apache 变量,这样我就不必指定实际的主机名,因为它只是一个没有关联域名的 IP 地址。

参考资料: https://stackoverflow.com/a/40291044/2089675


13
投票

实际上,您的主题属于 https://serverfault.com/ 但您仍然可以尝试检查这些 .htaccess 指令:

RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^(.*) https://%{HTTP_HOST}/$1

11
投票

如果您有 Apache2.4,请检查

000-default.conf
- 删除
DocumentRoot
并添加

Redirect permanent / https://[your-domain]/

7
投票

服务器版本:Apache/2.4.29 (Ubuntu)

在网上和apache的官方文档中进行了长时间的搜索后,唯一对我有用的解决方案来自/usr/share/doc/apache2/README.Debian.gz

To enable SSL, type (as user root):

    a2ensite default-ssl
    a2enmod ssl

在文件 /etc/apache2/sites-available/000-default.conf 中添加

重定向“/”“https://sub.domain.com/

<VirtualHost *:80>

    #ServerName www.example.com
    DocumentRoot /var/www/owncloud
    Redirect "/" "https://sub.domain.com/"

就是这样。


P.S:如果你想阅读手册而不解压:

gunzip -cd /usr/share/doc/apache2/README.Debian.gz

6
投票

此代码对我有用。

# ----------port 80----------
RewriteEngine on
# redirect http non-www to https www
RewriteCond %{HTTPS} off
RewriteCond %{SERVER_NAME} =example.com
RewriteRule ^ https://www.%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]

# redirect http www to https www
RewriteCond %{HTTPS} off
RewriteCond %{SERVER_NAME} =www.example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]

# ----------port 443----------
RewriteEngine on
# redirect https non-www to https www
RewriteCond %{SERVER_NAME} !^www\.(.*)$ [NC]
RewriteRule ^ https://www.%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]


5
投票

对我来说这很有效

RewriteEngine on
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

4
投票

请在 apache Virtualhosting 配置中尝试这个 然后重新加载apache服务

RewriteEngine On

RewriteCond %{HTTPS} off


RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI}

2
投票

这对我有用:

RewriteCond %{HTTPS} =off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [QSA,L,R=301]

-1
投票

Apache 文档中涵盖了此特定问题此处

要将

http
URL 重定向到
https
,请执行以下操作:

<VirtualHost *:80>
    ServerName www.example.com
    Redirect "/" "https://www.example.com/"
</VirtualHost>

<VirtualHost *:443>
    ServerName www.example.com
    # ... SSL configuration goes here
</VirtualHost>
© www.soinside.com 2019 - 2024. All rights reserved.