本文介绍了Apache HTTPD RewriteRule中的^和$是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已成功将以下代码添加到我的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有一些特殊字符,例如(,),[,] 等。
  • 上面的 RewriteRule 模式中的
  • ^ 只会返回每次匹配都为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.
    • ^ in RewriteRule 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.
    • 这里是:

      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]: if HOST_NAME doesn't start with www.
      • [NC,OR]: Ignore case match and ORs next condition
      • RewriteCond %{HTTPS} !on: HTTPS is not turned on
      • RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]: This condition will always match since www. is an optional match here. It is used to capture substring of HTTP_HOST without starting www. 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 to https://www.%1%{REQUEST_URI} with R=301 code by adding https:// and www. to %1. %1 is back-reference of capture group #1 from RewriteCond, as mentioned above.

      这篇关于Apache HTTPD RewriteRule中的^和$是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-27 10:52