Linux http 到 https apache 重定向

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16200501/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 22:47:30  来源:igfitidea点击:

http to https apache redirection

linuxapachewebserver

提问by Deano

EnvironmentCentos 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}/ [NC,R,L]

Any ideas?

有任何想法吗?

采纳答案by Deano

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

/etc/init.d/httpd restart

回答by 5ervant

Actually, your topic is belongs on https://serverfault.com/but you can still try to check these .htaccessdirectives:

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

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

回答by Jossef Harush

Searched for apache redirect http to httpsand landed here. This is what i did on ubuntu:

搜索apache redirect http to https并降落在这里。这是我在 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>
  • 请注意,SSL 模块需要证书。您需要指定现有的(如果您购买了)或自己生成自签名证书

3) Restart apache2

3)重启apache2

sudo service apache2 restart

回答by Fint

This worked for me:

这对我有用:

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

回答by user7817632

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]

回答by Vincy

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}/ [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}/ [R=301,L]

回答by indifference

If you have Apache2.4 check 000-default.conf- remove DocumentRootand add

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

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

回答by DimiDak

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.

就是这样。



P.S: If you want to read the manual without extracting:

PS:如果想看说明书而不解压:

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

回答by MD IRFAN

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}