2014-12-02 19 views
1

我有以下重写规则:“^”是什么和“!”意味着在Apache配置?

# 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 

将返回true,如果{REQUEST_URL}会喜欢:

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

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

请解释最后两个RewriteCond S:

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

+1

http://httpd.apache.org/docs/2.2/rewrite/intro.html – 2014-12-02 14:09:20

+0

我什么都看不到**!** – gstackoverflow 2014-12-02 14:12:07

+0

仔细一看:'在mod_rewrite中!可以在正则表达式之前使用字符否定它。“ – 2014-12-02 14:12:34

回答

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与?删除的查询字符串。

+0

这是什么意思** QSA **? – gstackoverflow 2014-12-02 14:32:36

+0

这是什么意思** $ 1 **? – gstackoverflow 2014-12-02 14:33:01

+1

这里记录http://httpd.apache.org/docs/2.4/rewrite/flags.html#flag_qsa – 2014-12-02 14:33:40

1
  • RewriteCond%{QUERY_STRING} ^。 ==>请求的查询字符串不是空的,具有至少一个字符
  • %{QUERY_STRING}!referrerPage =汽车回家==>查询字符串中不包含“referrerPage =汽车回家”