通过.htaccess将http重定向到https

时间:2016-12-16 17:57:43

标签: .htaccess redirect mod-rewrite

目前,我在论坛网站的.htaccess文件中有以下内容。

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteRule ^/?$ http://example.com/forums/ [L,R=301]

它非常适合重定向所有内容,例如:http://example.com,www.example.com,(http)//www.example.com等,这是我想要的example.com/forums

我现在希望将所有内容重定向到(https)://example.com/forums,但现在遇到问题,例如example.com/forums/stuff。

我只能将example.com重定向到https://example.com/forums,任何直接链接(例如example.com/forums/stuff等)都不会重定向到https版本,任何想法?

1 个答案:

答案 0 :(得分:0)

您需要2个重定向规则。将着陆页重定向到论坛页面的一条规则和另一条重定向http -> https的规则:

RewriteEngine On

# redirect landing page to /forums/
RewriteCond %{HTTP_HOST} ^(?:www\.)?example\.com [NC]
RewriteRule ^/?$ https://%{HTTP_HOST}/forums/ [L,R=301]

# redirect http -> https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
相关问题