HTTP到HTTPS重定向阿帕奇HTTP到HTTPS重定向阿帕奇(http to https apa

2019-05-08 15:36发布

环境的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]

有任何想法吗?

Answer 1:

其实,我跟着这个例子,它为我工作:)

NameVirtualHost *:80
<VirtualHost *:80>
   ServerName mysite.example.com
   DocumentRoot /usr/local/apache2/htdocs 
   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



Answer 2:

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



Answer 3:

搜索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


Answer 4:

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

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


Answer 5:

使用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维基- RewriteHTTPToHTTPS

如果你正在寻找一个301永久重定向,然后重定向标志应为,

 R=301

所以重写规则会是怎样,

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


Answer 6:

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

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


Answer 7:

这为我工作:

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


Answer 8:

服务器版本:阿帕奇/ 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/"

而已。


PS:如果你想阅读手册无需解压:

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


Answer 9:

这对我来说代码工作。

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



Answer 10:

请试试这个在apache的虚拟主机配置,然后重新装入Apache服务

RewriteEngine On

RewriteCond %{HTTPS} off


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


文章来源: http to https apache redirection