Forum Moderators: phranque

Message Too Old, No Replies

Exact parameter match in QUERY STRING

Match query parameter names, even when not the first

         

Abhihome

9:58 pm on Mar 17, 2010 (gmt 0)

10+ Year Member



Hi,

I am trying to get a pattern to satisfy following redirect scenarios.

I would need to Redirect the following URLs

'http://www.mysite.com/test.html?id=100
'http://www.mysite.com/test.html?test1=a00&id=100

'http://www.mysite.com/test.html?activity_id=100
'http://www.mysite.com/test.html?test1=a00&activity_id=100

and I would not redirect the following
'http://www.mysite.com/test.html
'http://www.mysite.com/test.html?test1=a00&xx_id=100&test2=200

i.e, the URL with parameter 'id=<value>' should redirect. But the URL with '<anything>id=<value>' should not. I am trying to find a RegEx that can do an exact match of 'id=<value>' anywhere in the URL as a Query Param.

Here is what I am trying to do.

RewriteCond %{HTTP_HOST} mysite\.com|www.\mysite\.com
RewriteCond %{QUERY_STRING} activity_id=|^id=
RewriteRule (.*) 'http://google.com?

This works fine for
'http://www.mysite.com/test.html?id=100
But not for
'http://www.mysite.com/test.html?test1=a00&id=100
Because, 'id=<value>' is not the first parameter in the URL.

Thanks
Abhilash

g1smd

10:58 pm on Mar 17, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Soft anchors like
[b]&?[/b]id=100[b]&?[/b]
might help you here.

Abhihome

12:51 am on Mar 18, 2010 (gmt 0)

10+ Year Member



Thanks for the quick response. But unfortunately that didn't work.

It is redirecting for
'http://www.mysite.com/test.html?test1=a00&xx_id=100&test2=200 too.

because the pattern &?id=&? matches xx_id=100 too.

I am looking for a pattern that can match id=<value> but not <something>id=<value>.

id=<value> can occur anywhere in the URL Params.

Thanks
Abhilash

Abhihome

1:12 am on Mar 18, 2010 (gmt 0)

10+ Year Member



\bid=\b seems to be working for me :)

Thanks
Abhilash

jdMorgan

2:43 am on Mar 18, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



For servers which do not support PCRE, you could use this:

RewriteCond %{QUERY_STRING} ^([^&]*&)*(activity_)?id=[0-9]+

Jim

Abhihome

2:57 am on Mar 18, 2010 (gmt 0)

10+ Year Member



Thanks Jim. I am closing this.

[SOLVED]