如何一次跳转所有请求到https和非www? [英] how can I Redirect all requests to https and non-www in one jump?
本文介绍了如何一次跳转所有请求到https和非www?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想一次将所有请求重定向到https和非www的主页/主页和其他子页面。
我正在使用以下htaccess。
我要这样:
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]
:如果HOST_NAME
以www开头。
-
[NC,OR]
:忽略大小写匹配和OR
的下一个条件 -
RewriteCond%{HTTPS}!on
:HTTPS
未打开 -
RewriteCond%{HTTP_HOST} ^(?: www\。)?(。+)$ [NC]
:此条件始终匹配,因为www。
是此处的可选匹配项。它用于捕获HTTP_HOST
的子字符串,而无需使用(。+)启动
模式(以后将被反向引用为www。
。捕获组#1中的%1
)。请注意,(?:..)
是一个非捕获组。 -
RewriteRule ^ https:/ /%1%{REQUEST_URI} [R = 301,L,NE]
:^
将始终匹配。该规则将使用R = 301
代码重定向到https://%1%{REQUEST_URI}
。如上所述,其中%1
是来自RewriteCond
的捕获组#1的反向引用。
I want to redirect all requests to https and non-www in one jump for main/ home page and other sub pages. I am using the following htaccess. source
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]
But I got the following redirection
I want like this:
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
解决方案
You may use this rule for both redirects in one rule:
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]
Here is the explanation of this rule:
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
: ifHOST_NAME
starts withwww.
[NC,OR]
: Ignore case match andOR
s next conditionRewriteCond %{HTTPS} !on
:HTTPS
is not turned onRewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
: This condition will always match sincewww.
is an optional match here. It is used to capture substring ofHTTP_HOST
without startingwww.
by using(.+)
pattern in capture group #1 (to be back-referenced as%1
later). Note that(?:..)
is a non-capturing group.RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]
:^
will always match. This rule will redirect tohttps://%1%{REQUEST_URI}
withR=301
code. Where%1
is back-reference of capture group #1 fromRewriteCond
, as mentioned above.
这篇关于如何一次跳转所有请求到https和非www?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文