简体   繁体   English

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

[英]How to automatically redirect HTTP to HTTPS on Apache servers?

Environment Centos with apache环境Centos with apache

Trying to setup automatic redirection from http to https尝试设置从 http 到 https 的自动重定向

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

I have tried adding the following to my httpd.conf but it didn't work我曾尝试将以下内容添加到我的 httpd.conf 但它没有用

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

Any ideas?有任何想法吗?

I have actually followed this example and it worked for me :)我实际上已经遵循了这个例子,它对我有用:)

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>

Then do:然后做:

/etc/init.d/httpd restart

Searched for apache redirect http to https and landed here.搜索apache redirect http to https并登陆这里。 This is what i did on ubuntu:这是我在 ubuntu 上所做的:

1) Enable modules 1) 启用模块

sudo a2enmod rewrite
sudo a2enmod ssl

2) Edit your site config 2) 编辑您的站点配置

Edit file编辑文件

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

Content should be:内容应该是:

<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>
  • Note that the SSL module requires certificate.请注意,SSL 模块需要证书。 you will need to specify existing one (if you bought one) or to generate a self-signed certificate by yourself.您需要指定现有的(如果您购买了)或自己生成自签名证书

3) Restart apache2 3)重启apache2

sudo service apache2 restart

Using mod_rewrite is not the recommended way instead use virtual host and redirect.使用 mod_rewrite 不是推荐的方式,而是使用虚拟主机和重定向。

In case, if you are inclined to do using 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

Reference: Httpd Wiki - RewriteHTTPToHTTPS参考: Httpd Wiki - RewriteHTTPToHTTPS

If you are looking for a 301 Permanent Redirect, then redirect flag should be as,如果您正在寻找 301 永久重定向,则重定向标志应为,

 R=301

so the RewriteRule will be like,所以 RewriteRule 会像,

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

Actually, your topic is belongs on https://serverfault.com/ but you can still try to check these .htaccess directives:实际上,您的主题属于https://serverfault.com/,但您仍然可以尝试检查这些.htaccess指令:

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

如果您有 Apache2.4 检查000-default.conf - 删除DocumentRoot并添加

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

This code work for me.这段代码对我有用。

# ----------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]

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

After long search on the web and in the official documentation of apache, the only solution that worked for me came from /usr/share/doc/apache2/README.Debian.gz在网上和 apache 的官方文档中长时间搜索后,唯一对我有用的解决方案来自/usr/share/doc/apache2/README.Debian.gz

To enable SSL, type (as user root):

    a2ensite default-ssl
    a2enmod ssl

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

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

<VirtualHost *:80>

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

That's it.就是这样。


PS: If you want to read the manual without extracting: PS:如果想看说明书而不解压:

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

This worked for me:这对我有用:

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

Please try this one in apache Virtualhosting configuration and then reload apache service请在 apache Virtualhosting 配置中尝试这个,然后重新加载 apache 服务

RewriteEngine On

RewriteCond %{HTTPS} off


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

I needed this for something as simple as redirecting all http traffic from the default apache home page on my server to one served over https .我需要它来做一些简单的事情,比如将所有http流量从我服务器上的默认 apache 主页重定向到通过https提供的主页。

Since I'm still quite green when it comes to configuring apache, I prefer to avoid using mod_rewrite directly and instead went for something simpler like this:因为我还是很绿色,当谈到配置Apache,我宁愿避免使用mod_rewrite直接,而是去了更简单的东西是这样的:

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

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

I like this because it allowed me to use apache variables and that way I didn't have to specify the actual host name since it's just an IP address without an associated domain name.我喜欢这个是因为它允许我使用 apache 变量,这样我就不必指定实际的主机名,因为它只是一个没有关联域名的 IP 地址。

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

for me this worked对我来说这有效

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

Environment Centos with apache apache的环境中心

Trying to setup automatic redirection from http to https尝试设置从http到https的自动重定向

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

I have tried adding the following to my httpd.conf but it didn't work我尝试将以下内容添加到我的httpd.conf中,但是没有用

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

Any ideas?有任何想法吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM