jsonfry
jsonfry

Reputation: 2087

Using .htaccess To Redirect URL With Parameters To URL With Paramters

I've been googling for a while trying to work out how to redirect the following url:

http://mysite.org/old-folder/?abc=1144

to a new URL:

http://mysite.org/new/folders/?xyz=1144

Where the number 1144 is dynamic.

I've got as far as this, but can't seem to get it working:

RewriteRule ^old-folder/\?abc=([0-9]+)(/)?$ new/folders/\?xyz=$1 [R=301]

Upvotes: 0

Views: 701

Answers (2)

jsonfry
jsonfry

Reputation: 2087

After some more research I have come up with this:

RewriteCond %{QUERY_STRING} ^abc=([0-9]+)$
RewriteRule ^old-folder new/folders/?xyz=%1 [L,R=301]

If anyone else comes to this in the future, it will only match values for 'abc' that are numeric. If you want it to match anything, replace the first line with this:

RewriteCond %{QUERY_STRING} ^abc=(.*)$

For an explanation of the flags [L,R=301] see the apache docs: http://httpd.apache.org/docs/current/rewrite/flags.html

Upvotes: 1

anubhava
anubhava

Reputation: 784868

You cant match query string in RewriteRule.

Try this rule in your DOCUMENT_ROOT/.htaccess file:

RewriteEngine On
RewriteBase /

RewriteCond %{QUERY_STRING} (?:^|&)abc=([^&]*) [NC]
RewriteRule ^old-folder/$ new/folders/?xyz=%1 [L,NC]

Upvotes: 0

Related Questions