0

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]
2
  • For the x-th time: RewriteRule only looks at the path component of the URL – if you want to check the query string, you have to use a RewriteCond Commented Feb 7, 2014 at 20:33
  • Thanks @CBroe. In my incredibly limited knowledge of this, I thought that was only if you were looking for a particular value of a parameter in a query string, but I was wrong. I'd seen bad examples on other sites that didn't use RewriteCond for query strings if they were dynamic. Commented Feb 8, 2014 at 13:14

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.