RewriteCond matches specific query parameter / value pairs

I need to do a redirect to another host if there is a parameter / value pair in the querystring.

I still have

RewriteCond %{REQUEST_URI}?%{QUERY_STRING} [&\?]abc=23&?
RewriteRule ^(.*)$ http://anotherserver.com/$1 [R,NC,L]

      

which works for:

/index.php?id=95&abc=23
/index.php?abc=23&id=95
/index.php?id=95&abc=23&bla=123

      

but also matches /index.php?id=95&abc=234

.

I want a pattern that matches exactly abc=23

no matter where it occurs.

Any suggestions on this? :-)

+2


source to share


2 answers


I would try this regex (&|^)abc=23(&|$)

and the match was against it %{QUERY_STRING}

.



+6


source


The question mark makes the previous token in the regex optional. For example: colou? R corresponds to a color or color.

RewriteCond %{REQUEST_URI}?%{QUERY_STRING} [&\?]abc=23&?

      

You match abc = 23 & OR abc = 23, the rest of the string is unbounded, so abc = 234 is a valid match. What you really want is there and nothing else. I'm not sure if this RegExp is legal in Apache, but it will be written as:



RewriteCond %{REQUEST_URI}?%{QUERY_STRING} [&\?]abc=23(&|$)

      

Here are the tests I used in my favorite online tester, RegExp :

abc=23&def=123
abc=234
abc=23

      

+1


source







All Articles