从http重定向301到https不起作用

时间:2014-05-10 13:59:43

标签: apache .htaccess redirect

我在我的共享虚拟主机中有这个.htaccess。

RewriteOptions inherit
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteRule ^(.*)$ %{ENV:BASE}index.php [NC,L]


RedirectMatch 301 ^/directory(.*)$ https://www.example.com/$1
RedirectMatch 301 ^/directory/(.*)$ https://www.example.com/$1
RedirectMatch 301 ^/directory\-2014\.03\.04(.*)$ https://www.example.com/$1
RedirectMatch 301 ^/directory\-2014\.03\.04/(.*)$ https://www.example.com/$1

RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^/?$ "https\:\/\/www\.example\.com\/" [R=301,L]

我希望所有拨打http://www.example.comhttp://example.com的电话重定向到https://www.example.com。但是我的设置也不起作用。

1 个答案:

答案 0 :(得分:0)

尝试更换:

RewriteCond %{HTTP_HOST} ^domain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.com$
RewriteRule ^/?$ "https\:\/\/www\.domain\.com\/" [R=301,L]

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]

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

以上内容首先会将http://domain.com更改为http://www.domain.com,如果不是http://www.domain.com,它会将https://www.domain.com重定向到https://