mod重写QUERY_STRING问题

时间:2012-10-25 22:34:17

标签: mod-rewrite apache2

所有

我正在将一些具有特定查询字符串的网址重定向到新页面。例如,http://testserver.xyz.com/abc/content/content.jsp?contentId=123http://testserver.xyz.com/abc/content/content.jsp?contentId=345需要重定向到http://ww2.newtestserver.com/xyz.html

为此,我写了以下重定向:

RewriteCond %{QUERY_STRING} contentId=123 [OR]
RewriteCond %{QUERY_STRING} contentId=345 [OR] 
RewriteCond %{QUERY_STRING} contentId=678 
RewriteRule ^/(.*)$ http://ww2.newtestserver.com/xyz.html/? [R=301,L]

除非我在浏览器http://testserver.xyz.com/abc/content/content.jsp?contentId=1234中输入内容,否则此功能正常。这也会被重定向到http://ww2.newtestserver.com/xyz.html

我不想要这个。我如何防止这种情况,以便我的mod重写只查看查询字符串123或345或567但不是123x或345x或678x?

请帮忙

TIA

1 个答案:

答案 0 :(得分:0)

为了确保重写只查看您指定的确切查询字符串,您应该使用^$告诉它这些完整查询字符串。 ^匹配字符串的开头,$匹配结尾。

RewriteCond %{QUERY_STRING} ^contentId=123$ [OR]
RewriteCond %{QUERY_STRING} ^contentId=345$ [OR]
RewriteCond %{QUERY_STRING} ^contentId=678$
RewriteRule ^/(.*)$ http://ww2.newtestserver.com/xyz.html [R=301,L]

以上代码可以使用。下面的代码可能有效(并且应该更有效),但我现在不能在我可以测试的计算机上。

RewriteCond %{QUERY_STRING} ^contentId=(123|345|678)$
RewriteRule ^/(.*)$ http://ww2.newtestserver.com/xyz.html [R=301,L]