Why is this regexp in .htaccess not matching?

Go To StackoverFlow.com

1

I have the following line:

RewriteRule ^page.php?var1=([0-9]+)&var2=(.*)&var3=(.*)$ index.php?module=page&var1=$1

When I visit the directory with this url: page.php?var1=24&var2=346adf&var3=asdf

It doesnt do the regexp. Anyone having a clue?

2012-04-05 16:00
by dirk


2

Basically your rule is incorrect. Reason: You can only match REQUEST_URI using RewriteRule but cannot match QUERY String in there.

Change your code to this to make it correct:

RewriteCond %{QUERY_STRING} ^var1=([0-9]+)&var2=[^&]*&var3=.*$ [NC]
RewriteRule ^page\.php/?$ index.php?module=page&var1=%1 [L,NC,QSA]
2012-04-05 16:05
by anubhava
Thank you very much - dirk 2012-04-05 16:13
Should ^page.php/?$ be ^page.php\? - David Gorsline 2012-04-05 16:17


1

The first thing I notice is that the ? should probably be escaped.

2012-04-05 16:03
by David Gorsline


1

Besides missing escapes, query string (the stuff after ?) is not part of the RewriteRule matching. You have to use an additional RewriteCond if you want to match on the query string. For example:

RewriteCond %{QUERY_STRING} ^foo=([^&]+)&bar$
RewriteRule ^page\.php$ otherpage.php?x=%1 [QSA]

I wouldn't recommend this tho. You are better off parsing the query string in your scripts.

2012-04-05 16:05
by Qtax
See also http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritecon - Qtax 2012-04-05 16:11
Ads