开发者

What RewriteRule would be to redirect based on the on query string parameters?

If requested page page1.html and in query string uin is anything but not 12 or 13, let them see this page1.html page, otherwise redirect them to page2.html


Update: BTW, there are also other params in the query string. They should be s开发者_运维技巧ent to either page too.


The Rewrite Condition you're looking for is %{QUERY_STRING}

Here's another SO question doing something similar: Redirecting URLs (with specific GET parameters)


This will redirect to page2.html if uin=12 or uin=13. The entire query string will be sent to the page2.html page:

 # EDIT: Doesn't properly handle all cases
 RewriteCond %{QUERY_STRING}  [\&]+uin=1[23][&]+ [OR]
 RewriteCond %{QUERY_STRING}  ^uin=1[23][&]+
 RewriteRule ^/page1\.html   /page2.html  [R]

EDIT: This is a lot better and will handle the parameter in any position in the query string, beginning or end, and will also account for filtering out cases where the string is within another parameter, like suin=123

 RewriteCond %{QUERY_STRING} ^(.*&)*uin=1[23](&.*)*$
 RewriteRule ^/page1\.html   /page2.html  [R]

I tested on the following cases:

Redirected:
http://local.sandbox.com/page1.html?hello=world&uin=13&test=1
http://local.sandbox.com/page1.html?uin=12&test=1
http://local.sandbox.com/page1.html?uin=12
http://local.sandbox.com/page1.html?uin=13
http://local.sandbox.com/page1.html?uin=13&t=t
http://local.sandbox.com/page1.html?t=t&r=r&uin=13&t=3
http://local.sandbox.com/page1.html?t=t&uin=13

Didn't redirect:
http://local.sandbox.com/page1.html?uin=11&test=1
http://local.sandbox.com/page1.html?hello=world&uin=1&test=1
http://local.sandbox.com/page1.html?hello=world&ui=13&test=1
http://local.sandbox.com/page1.html?t=t&&r=r&suin=13&t=3
http://local.sandbox.com/page1.html?t=t&&r=r&uin=134&t=3
http://local.sandbox.com/page1.html?suin=134&t=3
http://local.sandbox.com/page1.html?auin=13&t=t
http://local.sandbox.com/page1.html?uin=134&t=3
http://local.sandbox.com/page1.html?t=t&uin=134
http://local.sandbox.com/page1.html?t=t&auin=13

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜