我要把 http://www.abc.com/a/b?id=123 的一條鏈接轉換成 http://www.abc.com/p-123.html
想當然就以爲問號還是按照普通字符用正則表達式轉義來處理,類似 b\?id= 這樣,誰知道根本不起作用。
後來在谷歌幫助下才發現原來問號在nginx中是有特殊邏輯的,它用$query_string來表示問號以後的字符,即"id=123"
這樣就好辦了,換個方式就ok了。
最後是這樣寫的:
if ($request_uri ~* "^/a/b\?id=(\d+)$") {
set $myarg1 $1;
rewrite .* http://www.abc.com/p-$myarg1.html? permanent;
}
注意,set $myarg1 $1; 這句話不能少,不能直接用$1,會出錯的。