[樂意黎原創]Nginx裡帶引數的rewrite詳解
阿新 • • 發佈:2018-11-20
1. 如下所示,帶 www.daza.ren/view-detail-weixin-9999.html 和 www.daza.ren/view-detail-weixin-9999.html?65 在apache 下的寫法。
1). www.daza.ren/view-detail-weixin-9999.html
用 apache httpd htacess 的寫法:
RewriteRule ^[a-zA-Z]*/detail[/-]?([a-zA-Z$]*)[/-]?([0-9a-zA-Z]*)\.html$ detail-comm.php?type=$1&ids=$2
2). www.daza.ren/view-detail-weixin-9999.html?65
用 apache httpd htacess 的寫法:
RewriteCond %{QUERY_STRING} ^(.+)$
RewriteRule ^[a-zA-Z]*/detail[/-]?([a-zA-Z]*)[/-]?([0-9a-zA-Z]*)\.html$ detail-comm.php?type=$1&ids=$2&showNumber=%1
2. 現因採用Nginx ,要把形如 www.daza.ren/view-detail-9999.html 的url 改用nginx 裡的 rewrite 來寫。
2.1). www.daza.ren/view-detail-weixin-9999.html
用 nginx rewrite的寫法:
rewrite "^/[a-zA-Z]*/detail[/-]?([a-zA-Z$]*)[/-]?([0-9a-zA-Z]*)\.html$" /detail-comm.php?type=$1&ids=$2;
2.2). www.daza.ren/view-detail-weixin-9999.html?65
用 nginx rewrite的寫法:
rewrite "^/[a-zA-Z]*/detail[/-]?([a-zA-Z$]*)[/-]?([0-9a-zA-Z]*)\.html$" /$1/$2/$args?; rewrite "^/([a-zA-Z]*)[/-]?([a-zA-Z0-9]*)[/-]?([0-9]*)?" /detail-comm.php?type=$1&id=$2&showNumber=$3 last;
轉發後,傳遞的url 如下圖. 注意字尾加?與未加? 的區別。
2.2.1) 下圖加未加 ?.
2.2.2) 下圖加了 ?.
注: 關鍵點就在於“?”這個尾綴。重定向的目標地址結尾處如果加了?號,則不會再轉發傳遞過來原地址的問號?後面的引數部分(如上面的 &45)。
注: nginx rewrite正則匹配不會匹配問號後的引數,因此需要使用$arg_{引數名}來保留引數, $args 表示泛引數,且匹配規則要以問號結尾;
2.3). www.daza.ren/view-detail-typename-xxxx.html?pageno=45
用 nginx rewrite的寫法:
rewrite "^/[a-zA-Z]*/detail[/-]?([a-zA-Z$]*)[/-]?([0-9a-zA-Z]*)\.html$" /$1/$2/$arg_pageno?;
rewrite "^/([a-zA-Z]*)[/-]?([a-zA-Z0-9]*)[/-]?([0-9]*)?" /detail-comm.php?type=$1&id=$2&showNumber=$3 last;
轉發後,傳遞的url 如下圖.
2.4). 上述情況,如在 rewrite 語句 後加 permanent, 則會地址轉向.
3. 首頁匹配,可用.
rewrite "^(/|/index\.html|/index\.php)$" /index.php last;
樂意黎原創
本文地址: https://blog.csdn.net/aerchi/article/details/84068768