如何将https:// www重定向到https://并将http:// www重定向到https://并将http://重定向到https://

时间:2016-09-28 07:38:16

标签: .htaccess redirect

我需要重定向以下所有地址:

  • http://www.example.com
  • https://www.example.com
  • http://example.com

https://example.com使用.htaccess文件中的永久重定向。

1 个答案:

答案 0 :(得分:1)

我会尝试以下方法:

RewriteEngine On

# URL with www rewrite to https without www
RewriteCond %{HTTP_HOST} ^(www\.)(.*) [NC]
RewriteRule (.*) https://%2%{REQUEST_URI} [L,R=301]

# URL without www rewrite to https
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^(www\.)(.*) [NC]
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]