【轉載】Apache Rewrite處理?問號後的請求參數

原文地址:http://www.netroby.com/html/2011/techdocument_0509/138.html

最近把一個項目從nginx轉移到apache下面,有些rewrite規則?問號後面還有請求參數,apache rewrite規則需要改變一下才能適應這種需求。

最近把一個項目從nginx轉移到apache下面,有些rewrite規則?問號後面還有請求參數,apache rewrite規則需要改變一下才能適應這種需求。

我們Rewrite後的地址 /sj/php?page=8, 實際地址 /search-job.php?key=php&page=8

在apache rewrite文檔裏面有一段介紹[QSA]參數,專門用來傳遞?後的請求參數到實際地址。

http://httpd.apache.org/docs/current/rewrite/flags.html#flag_qsa

 

When the replacement URI contains a query string, the default behavior of RewriteRule is to discard the existing query string, and replace it with the newly generated one. Using the [QSA] flag causes the query strings to be combined.

 

 

Consider the following rule:

RewriteRule /pages/(.+) /page.php?page=$1 [QSA]

With the [QSA] flag, a request for /pages/123?one=two will be mapped to /page.php?page=123&one=two. Without the [QSA] flag, that same request will be mapped to/page.php?page=123 - that is, the existing query string will be discarded.

 

我們由此可以知道我們的規則應該這樣寫:

RewriteRule /sj/(.+) /search-job.php?key=$1 [QSA]

apache的Rewrite規則可以實現很複雜的需求,但由此而來的問題就是它不是完全遵守RegEx正則表達式的規範。

nginx的rewrite很優雅,也比較簡單,雖然它不能實現非常複雜的Rewrite,但應付日常應該足夠。

我個人還是比較喜歡nginx的rewrite的寫法,因爲它是標準的regex正則表達式。

apache Rewrite規則不好調試,因爲它很多時候是不標準的Regex.

原文地址:http://www.netroby.com/html/2011/techdocument_0509/138.html

參考文檔:

http://wymsxty.blog.163.com/blog/static/77790858201121112725596/

http://man.chinaunix.net/newsoft/ApacheManual/misc/rewriteguide.html

http://lamp.linux.gov.cn/Apache/ApacheMenu/rewrite/rewrite_guide.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章