什么" ^。"和"!"在apache配置中意味着什么?

时间:2014-12-02 14:08:36

标签: apache mod-rewrite

我有以下重写规则:

   # Rewriting without query parameters to avoid cache overloading
    RewriteCond %{REQUEST_URI} /(en|fr)/search-results.html
    RewriteCond %{QUERY_STRING} ^.
    RewriteCond %{QUERY_STRING} !referrerPage=automotive-home
    RewriteRule ^(.*)/search-results.html$ $1/search-results.html? [NC]

据我了解

RewriteCond %{REQUEST_URI} /(en|fr)/search-results.html
如果{REQUEST_URL}愿意,

将返回true:

https://www.trololo.com/en/search-results.html

https://www.trololo.com/fr/search-results.html

请解释最后两个RewriteCond

RewriteCond %{QUERY_STRING} ^.
RewriteCond %{QUERY_STRING} !referrerPage=automotive-home
  1. RewriteCond %{QUERY_STRING} ^. 这是否意味着QUERY_STRING不是空白

  2. %{QUERY_STRING} !referrerPage=automotive-home 这是否意味着QUERY_STRING不包含referrerPage=automotive-home

2 个答案:

答案 0 :(得分:1)

正则表达式^.表示匹配任何一个字符 . The ^`本身表示字符串的开头,对于这样的通用表达式通常不需要;它本可以省略

.匹配任何一个字符 ...因此,在此上下文中,它表示查询字符串必须至少包含1个字符;如果查询字符串为空,则不满足条件。

# If the requested query string is *not empty, having at least one character*
RewriteCond %{QUERY_STRING} ^.

# ...and the query string does not contain "referrerPage=automotive-home"
# It doesn't need to be the complete expression because it is not anchored
# with ^ at the start and $ at the end, so this pattern will match
# if it appears anywhere in the query string
RewriteCond %{QUERY_STRING} !referrerPage=automotive-home

# If the above 2 conditions were met, the next `RewriteRule` will be processed.
# This rewrite rule's purpose is to erase the query string. Since it terminates in 
# ? without a [QSA] flag, any existing query string will be removed
RewriteRule ^(.*)/search-results.html$ $1/search-results.html? [NC]

在这种情况下,第一个RewriteCond只能在没有^

的情况下表达
RewriteCond %{QUERY_STRING} .

正如评论中所提到的,!否定了后续的表达。这个以及锚点和.字符记录在the mod_rewrite regex vocabulary.

最后,从Apache 2.4开始,有a [QSD] ("query string discard") flag与使用?结束目标URI以擦除查询字符串相同。

答案 1 :(得分:1)

  • RewriteCond%{QUERY_STRING} ^。 ==>请求的查询字符串不为空,至少有一个字符
  • %{QUERY_STRING}!referrerPage = automotive-home ==>查询字符串不包含" referrerPage = automotive-home"