2016-10-11 102 views
1

我想將http重定向到https和www到非www。http到https,www到非www和尊重url

案例:

http://example.com/my/url 
http://www.example.com/my/url 
https://example.com/my/url 
https://www.example.com/my/url 

結果

https://example.com/my/url 
https://example.com/my/url 
https://example.com/my/url 
https://example.com/my/url 

我現在的htaccess文件:

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

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

問題:

它重定向到https://example.com/index.php

我想讓它尊重網址https://example.com/my/url

我發現很多問題,但我還沒有找到完整的解決方案。事實上,上面的htaccess代碼是唯一能夠像10次嘗試一樣運行良好的代碼。

回答

2

您可以使用此單一的規則去除wwwhttp -> https

RewriteEngine On 

RewriteCond %{HTTP_HOST} ^www\. [NC,OR] 
RewriteCond %{HTTPS} off 
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC] 
RewriteRule^https://%1%{REQUEST_URI} [R=301,L,NE] 

# rest of rules go here .... 

如果你有太多的重定向的問題,嘗試更換第二行:
RewriteCond %{ENV:HTTPS} !on

這是重要的是保持此規則作爲您的第一個規則並在測試之前清除瀏覽器緩存。

+1

很高興擁有一切規則。我認爲解決辦法是把它放在任何其他規則之前。謝謝! –

相關問題