如何重定向https://开头WWW到https://和http:// WWW到https://和http://到https://

问题描述:

我需要重定向所有下列地址:如何重定向https://开头WWW到https://和http:// WWW到https://和http://到https://

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

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

+2

有几个答案这对SO。你有尝试过什么吗? – anubhava

我会尝试以下方法:

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] 
+1

您在'RewriteRule' _pattern_中没有捕获组(匹配'$ 1'),(或者将反向引用改为$ 0')。但是这个重定向'http:// example.com'也不会。 – MrWhite

+0

你是对的,错过了那部分。我编辑了我的帖子。谢谢! –

+0

+1 Minor tidy ...根据上述规则,第二个规则集中的'RewriteCond%{HTTP_HOST} ...'指令似乎是多余的。除非需要捕获子模式,否则不需要括号。 (无论如何,你实际上无法在否定的正则表达式中捕获组。) – MrWhite