Anders
Anders

Reputation: 499

.htaccess 301 redirect with variables

I'm upgrading a site which will use new links. It is possible to figure out the new URL from the old URL so I want to make a few 301 entries in the .htaccess to forward old links.

I'm aware that this is possible

Redirect 301 /orginalURL.html http://website.com/newURL.php

However I need to use portions of the old url to construct the new url. Is this possible? Example old url

/index.php?page=/shop/bcat&c=47

And that should instead be

/se/category/47

In this case 47 is the key that I need to get from the old URL to the new. Also I can't expect /index.php to exist in all old links so they might look like /?page=/shop/bcat&c=47 as well.

Anyone who can help me here and I might be able to figure out the rest of the redirects myself :)

Upvotes: 1

Views: 1893

Answers (1)

Jon Lin
Jon Lin

Reputation: 143886

You'll need to use mod_rewrite to match against the query string. Try something like this in the htaccess file in your document root:

RewriteEngine On
RewriteCond %{QUERY_STRING} page=/shop/bcat&c=([0-9]+)
RewriteRule ^/?(index.php)?$ http://website.com/se/category/%1? [L,R=301]

Upvotes: 4

Related Questions