使用.htaccess重定向到https

时间:2014-08-07 20:38:59

标签: .htaccess redirect ssl certificate

我的.htaccess有问题

我的服务器中有证书,并且仅适用于https://example.com(不是https://www.example.com)因此,我尝试将.htaccess重定向到正确的网址

我的意图是:

http://www.example.com --> https://example.com
http://example.com --> https://example.com
https://www.example.com --> https://example.com

我尝试了不同的组合,似乎没有任何效果。目前我有这个,但似乎不适合

http://www.example.com/subfolder,我不知道失败的原因......

RewriteEngine On

# Follow symbolic links.
Options +FollowSymLinks

RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

# Prevent directory listings
Options All -Indexes

2 个答案:

答案 0 :(得分:2)

该代码对我不起作用,我发现,这对我有用

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]

答案 1 :(得分:1)

用这个替换你当前的代码

Options -Indexes +FollowSymLinks
RewriteEngine On

# redirect "www" domain to https://example.com
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

# redirect http to https (at this point, domain is without "www")
RewriteCond %{HTTPS} =off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]