Forum Moderators: phranque

Message Too Old, No Replies

301 redirect with 2 query variables in url?

         

meade999

12:27 am on Jun 23, 2012 (gmt 0)

10+ Year Member



Hi Guys,

I am trying to create a 301 redirect in htaccess and realised that apache doesnt allow queries in urls when doing a simple 301.

I now realise that I have to create some kind of rule but rewrites and regex and not at the top of my CV! :-)

I was wondering if anyone could help me 301 redirect this:

http://www.example.com/little-thoughts/details.php?id=117&theme_id=5

TO THIS:

http://www.example.com/an-example-product.html



I have a list of these and the id's and theme_id's are all different and each URL needs to be 301'd to another URL.


Just want to mention, that this is a Magento store, and I already have this in the htaccess file, so not sure if any additional rules would conflict with this?:


Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^example.com [nc]
rewriterule ^(.*)$ http://www.example.com/$1 [r=301,nc]



Any help would be massively appreciated!

Thanks

Mike

g1smd

12:36 am on Jun 23, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You'll need to internally rewrite (that's rewrite, not redirect) the parameterised requests to a PHP script.

That script will look up the new URL from an array, file, or database table, and send a 301 header with that data included.

htaccess cannot do these redirects on its because it has no way to know the new name for the old numerical ID for each URL.

Additionally, the final page generation script will need to look up which theme should be used for this page. The theme ID should NOT be a part of the URL.

meade999

12:48 am on Jun 23, 2012 (gmt 0)

10+ Year Member



Hi, thanks for the reply. Well when I say I have a list, it's tens, not thousands. I was just going to do a list of 301s in the htaccess file, I have the old url and new url for each one. But obviously the query doesn't work.

Can I not do a rewrite of ?id=1&theme_id=5 to /1/5 for example. And then do a 301 from /1/5 to the new url?

Thanks
Mike

incrediBILL

1:43 am on Jun 23, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You just need to create a rewrite condition for each query string combination and then apply a rewrite rule to redirect to the destination URL.

For instance your original example would be something like:

RewriteCond %{QUERY_STRING} ^id=117&theme_id=5$ [NC]
RewriteRule (.*) /an-example-product.html [L,R=301]

If you only have a few, this is an easy way to go.

If you have a bunch, do it in PHP like g1smd said

lucy24

7:12 am on Jun 23, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Heh. I think everyone is talking at cross purposes :)

I am trying to create a 301 redirect in htaccess and realised that apache doesnt allow queries in urls when doing a simple 301.

This is only true if by "simple 301" you mean a Redirect using mod_alias. You can do it easily in mod_rewrite. You can change the query, delete the query, add to the query, or (the default) carry it over unchanged.

Do a Forums search for "rewrite query boilerplate" for more than you ever wanted to know.