Question

I am trying to figure out how to rewrite URLs from something like this: example.com/collection.php?collection=1

Into: example.com/collection-name.php

I recently redesigned an e-commerce site and need to redirect the old URLs to the new ones. I've seen loads of instructions on how to use the value of collection=1 in a rewritten URL but not how to use text instead of the value. There are only 5 collection values that need to be redirected but in addition there are also old URLs that have multiple params in them that also need to be rewritten/redirected as text. I hop that made sense.

So I think I can get them all worked out if I can get the initial redirect set up.

Other/more complex old URLs look like this: example.com/collection.php?collection=1&product=2&item=3

Which would then need to be redirected to: example.com/collection-name/sub-collection-name/product-name.php

Not sure exactly how to go about doing this. I don't want to write line after line in the .htaccess file but I have no idea of how else to accomplish this.

Thanks in advance, I appreciate andy and all help in this matter!

EDIT------------------------------------ Here's my new rewrite condition and rule based on the info provided to me. This would be the rule for the URLs containing all the query params using 3 separate RewriteMaps.

RewriteCond %{QUERY_STRING} collection=([^&]+)&product=([^&]+)&item=([^&]+)
RewriteRule collection.php shop/${categorymap:%1}/${rangemap:%2}/${productmap:%3}\.php [R=301,L]

Please let me know if anything looks off or I missed anything. Wasn;t sure if I needed to use $1, $2, $3 for each of the query params. I'm still a NOOB with rewrites. Thanks!

Edit------------------------------------------------------

Using the following code in my .htaccess file:

RewriteCond %{QUERY_STRING} collection=([^&]+)
RewriteRule collection.php shop/${categorymap:%1}\.php [R=301,L]

My URLs that start as "example.com/collection.php?collection=1

Are being rewritten as: example.com/shop/.php?collection=1

I am a bit lost on this one. Could it be that my Redirect Maps are not being read? Or is it something else? Thanks for the help.

Was it helpful?

Solution

You want to look into using a RewriteMap. You can specify the maps you want for collection, sub-collection and products, and then look up the text for each number.

So you would define a RewriteMap in your virtualhost config with something like

RewriteMap categmap txt:/path/to/category/map.txt

Then your rewrite rule would look something like

RewriteCond %{QUERY_STRING} collection=([^&]+)
RewriteRule collection.php ${categmap:%1} [L,R]

Add more RewriteConds for your more complicated cases and make separate rules out of them. Place the more specific rules first in the file, then the more general ones.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top