Apache RewriteRule传递参数时遇到的问题

页面的url pattern原本是:http://www.host.itd/catalog/product.do?id=8978,
但是为了做SEO,要将url pattern重写成http://www.host.itd/SDFD-DFD-DCDFD/ip/8978,
就在配置文件中定义了一个rewrite rule:
RewriteRule  ^(.*)/(.+)/ip/(/d+)     /catalog/product.do?id=$3
但是,发现了一个问题,因为这些页面上有一些相对url如:/other.do?id=1234,
因为这个RewriteRule的存在,这种类型的请求就不能正常工作了,原来的url应该是
http://www.host.itd/catalog/other.do?id=1234,
却被重写成了
http://www.host.itd/SDFD-DFD-DCDFD/ip/other.do?id=1234;
想到用如下的方式解决:
再定义一个RewriteRule,将那些相对url重写回原始的url。
RewriteRule ^(.*)/(.+)/ip/(.+)/?id=(/d+)     /catalog/$3?id=$4
但是却发现这个rule没有生效,查了下log文件,发现url上的参数id=(/d+)在重写的时候丢掉了。
于是就到apache官方网站上去查了下,找到问题的原因了。
官网上在讲到RewriteRule有这么一段话:
What is matched?
The Pattern will initially be matched against the part of the URL after the hostname and port, and before the query string.
If you wish to match against the hostname, port, or query string, use a RewriteCond with the %{HTTP_HOST},
%{SERVER_PORT}, or %{QUERY_STRING} variables respectively.
主要意思是:定义的样式只匹配url的主机名和端口之后,查询字串前的部分,如果想要匹配主机名,端口,查询字串,
就要使用RewriteCond结合如下变量%{HTTP_HOST},%{SERVER_PORT}, %{QUERY_STRING}.
ok,问题找到了,定义如下的rule:
RewriteCond  %{QUERY_STRING}%      ^(id=/d+)
RewiteRule     ^(.*)/(.+)/ip/([^/d]+)        /catalog/$3?%1 
也可以只定义如下的rule,其效果和上面是相同的
RewriteRule ^(.*)/(.+)/ip/([^/d]+)     /catalog/$3?%{QUERY_STRING}%
问题解决。

你可能感兴趣的:(apache,server,String,url,query,variables)