簡體   English   中英

如何一次性將所有請求重定向到 https 和非 www ?

[英]how can I Redirect all requests to https and non-www in one jump?

我想將所有請求重定向到 https 和非 www 主頁/主頁和其他子頁面的一次跳轉。 我正在使用以下 htaccess。 來源

RewriteEngine On
RewriteBase /

RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^mytesting\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.mytesting\.com$
RewriteRule .* https://www.mytesting.com%{REQUEST_URI} [R=301,L]

但我得到了以下重定向

在此處輸入圖片說明

我想要這樣:

http://mytesting.com          > https://mytesting.com
http://www.mytesting.com      > https://mytesting.com
http://mytesting.com/faq      > https://mytesting.com/faq
https://mytesting.com         > https://mytesting.com
http://www.mytesting.com      > https://mytesting.com
http://www.mytesting.com/faq  > https://mytesting.com/faq
https://mytesting.com/faq     > https://mytesting.com/faq

您可以在一個規則中為兩個重定向使用此規則:

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

下面是這個規則的解釋:

  • RewriteCond %{HTTP_HOST} ^www\\. [NC,OR] RewriteCond %{HTTP_HOST} ^www\\. [NC,OR] : 如果HOST_NAMEwww.開頭www.
  • [NC,OR] : 忽略大小寫匹配和OR的下一個條件
  • RewriteCond %{HTTPS} !on : HTTPS未開啟
  • RewriteCond %{HTTP_HOST} ^(?:www\\.)?(.+)$ [NC] :此條件將始終匹配,因為www. 在這里是可選匹配。 它用於在不以www.開頭的情況下捕獲HTTP_HOST子字符串www. 通過在捕獲組 #1 中使用(.+)模式(稍后將被反向引用為%1 )。 請注意, (?:..)是一個非捕獲組。
  • RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE] : ^將始終匹配。 此規則將使用R=301代碼重定向到https://%1%{REQUEST_URI} 其中%1是來自RewriteCond的捕獲組 #1 的反向引用,如上所述。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM