原文地址:http://www.netroby.com/html/2011/techdocument_0509/138.html
最近把一个项目从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