Question

It seems rewritemap is not considering query params in matching url. Please suggest a solution.

My setup in httpd.conf file:

RewriteMap redirects dbm=db:/usr/local/apache/conf/redirects.db
RewriteCond ${redirects:$1} !=""
RewriteRule ^(.*)$ ${redirects:$1} [redirect=permanent,last]

redirects file has urls lile

/same_url/ http://mysite.com/
/same_url/?q=1 http://mysite.com/q2=1
/same_url/?q=2 http://mysite.com/q2=2
/same_url/?q=3 http://mysite.com/q2=3

But all 4 urls are getting directed to http://mysite.com only. So it seems matching is only done against non-query part.

Please help.

Was it helpful?

Solution

Looking at your RewriteMap it seems that you do not need to use a map. You can do it without a map:

RewriteCond %{QUERY_STRING} ^q\=([0-9]+)$
RewriteRule ^/same_url/(.*)$ http://example.com/q2=%1 [redirect=permanent,last]

Pay attention to the %1 in the RewriteRule backreferencing to the match in the RewriteCond.

If you still want to use a Rewrite map you might have to "transform" the query string into something in the path The rules could be:

RewriteCond %{QUERY_STRING} ^q\=([0-9]+)$
RewriteRule ^/(.*)$ /$1/q=%1   <-- here the GET param is transformed to something in the path

RewriteMap redirects dbm=db:/usr/local/apache/conf/redirects.db
RewriteCond ${redirects:$1} !=""
RewriteRule ^(.*)$ ${redirects:$1} [redirect=permanent,last]

You'll have to change your map to something not considering GET params:

/same_url/ http://example.com/
/same_url/q=1 http://example.com/q2=1
/same_url/q=2 http://example.com/q2=2
/same_url/q=3 http://example.com/q2=3
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top