使用htaccess将所有网址重定向到https(停放和子网域除外)

时间:2017-01-27 14:10:55

标签: apache .htaccess redirect https

我在网站上安装了ssl,需要将所有页面重定向到https。我有一些问题:

1 - 我有一个停放的域example.com,我想重定向到example.net(主域),匹配任何网址,例如example.com/image1.jpg到example.net/image1.jpg是否它包括www或不包括。

2 - 我还想从https重定向中排除一个指向test.example2.net的域名test.com(实际上是一个子域名)。

我之前已经开始工作,在堆栈溢出时发现了一些代码,但我的最终代码很长,重定向次数多于所需的重定向,我认为不一定是这样。

非常感谢任何帮助。

以下代码适用于所有example.net网址。

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

1 个答案:

答案 0 :(得分:1)

您可以使用以下规则:

RewriteEngine on
#exclude test.com
RewriteCond %{HTTP_HOST} !test\.com [NC]
#http to https
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(www\.)?.+ [NC]
RewriteRule ^ https://%1example.net%{REQUEST_URI} [NE,L,R]
相关问题