2014-12-02 87 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 =汽車回家」