问题描述
我已成功将以下代码添加到我的Apache HTTPD配置中:
I have successfully added the following code to my Apache HTTPD configuration:
# Force www.
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
# Force https (SSL)
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
虽然可以正常工作,但我有一个理论上的问题:
Although it works as expected, I have a theoretical question:
为什么会有 ^
和 $
在第三行中强制执行 www。,而不在第六行中强制执行 https?
Why are there a ^
and $
in 3rd line enforcing "www.", and not in the 6th line enforcing "https"?
此致,Dovid。
推荐答案
对于您的两个正则表达式模式 ^(。*)$
和(。*)
的行为相同。不过,请猜测,您不需要使用任何一个。实际上,不使用。*
并使用与匹配的%{REQUEST_URI}
变量的错误发生率要低得多。完整URI(不是相对的URI,例如。*
)。因此,我建议将您的规则更改为:
For both of your regex patterns ^(.*)$
and (.*)
will behave same. However guess what, you don't need to use any of them. In fact it is far less error prone also to not to use .*
and use %{REQUEST_URI}
variable that matches full URI (not the relative one like .*
). So I suggest change your rules to this:
# Force www.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
# Force https (SSL)
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
- 标志
NE
用于避免转义。如果您的原始URI有一些特殊字符,例如#
或(,),[,]
等。 上面的 -
^
只会返回每次匹配都为true,因为^
表示字符串的开始位置,并且始终匹配。 - 两个规则都可以组合为一个
- Flag
NE
is used for not escaping. It is useful to have this flag in case your original URI has some special characters like#
or(,),[,]
etc. ^
inRewriteRule
pattern above does nothing but returns true for every match since^
means start position of a string and it will be always match.- Both rules can be combined into a single rule but it will look a bit complicated.
RewriteRule
模式中的这里是:
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%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:/ /www.%1%{REQUEST_URI} [R = 301,L,NE]
:^
将始终匹配。该规则将通过添加到R = 301
代码重定向到https://www.%1% {REQUEST_URI}
https://
和www。
到%1
。如上所述,%1
是RewriteCond
中捕获组#1的后向引用。
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
: ifHOST_NAME
doesn't start 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://www.%1%{REQUEST_URI} [R=301,L,NE]
:^
will always match. This rule will redirect tohttps://www.%1%{REQUEST_URI}
withR=301
code by addinghttps://
andwww.
to%1
.%1
is back-reference of capture group #1 fromRewriteCond
, as mentioned above.
这篇关于Apache HTTPD RewriteRule中的^和$是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!