我試圖找出一個.htaccess規則,我可以在我的主域&子域名上使用,而不需要更改每個子域的代碼。動態強制HTTPS和非WWW的子域名
我試過以下的子域嘗試和強制https和非www,但它不能正常工作。
<IfModule mod_rewrite.c>
# Force HTTPS & NON-WWW
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.test\.website\.com$ [NC] # Detect if it has www?
RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{HTTP_HOST} !^test\.website\.com$ [NC]
RewriteRule^https://test\.website\.com%{REQUEST_URI} [R=301,L]
</IfModule>
目前,如果我是去http://www.test.website.com/test/
它重定向到https://test%2Cwebsite.com/
所以它有點工作,但不完全是。
我一直在試圖做更多的動態也使用:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase/
RewriteCond %{HTTPS} !=on [NC]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
</IfModule>
但是這是行不通的。達到這個目標的最佳解決方案是什麼?
應該基本上可以強迫所有如下:
http://website.com/ => https://website.com/
http://www.website.com/ => https://website.com/
https://www.website.com/ => https://website.com/
http://website.com/testing/ => https://website.com/testing/
http://www.website.com/testing/ => https://website.com/testing/
https://www.website.com/testing/ => https://website.com/testing/
http://test.website.com/ => https://test.website.com/
http://www.test.website.com/ => https://test.website.com/
https://www.test.website.com/ => https://test.website.com/
http://test.website.com/testing/ => https://test.website.com/testing/
http://www.test.website.com/testing/ => https://test.website.com/testing/
https://www.test.website.com/testing/ => https://test.website.com/testing/
「它重定向到'https:// test%2Cwebsite.com /'」 - 有一些奇怪的事情發生在那裏,它不涉及您在上面發佈的指令。 '%2C'是一個編碼的逗號?!那來自哪裏? – MrWhite
@ w3dk我也無法理解%2C,網址中沒有逗號,但它正在放置一個。 – Ryflex