1

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.

Chris Morgan
  • 86,207
  • 24
  • 208
  • 215
dkmojo
  • 125
  • 5

1 Answers1

1

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.

Ansari
  • 8,168
  • 2
  • 23
  • 34
  • Awesome! Thank you for your help. The RewriteMap seems pretty straight forward. Just need to look into VirtualHost to see how that is structured as I'v not needed to use it until now. Thanks again! – dkmojo May 30 '12 at 14:36
  • Quick question, since my old params can have the same value (collection=1&product=1&item=1) would I then need mutltiple RewriteMap files for each of these params? I am thinking yes but am unsure. Thanks – dkmojo May 30 '12 at 14:50
  • I suggest three map files - one for each variable, especially since the values can overlap and mean different things in different contexts. – Ansari May 30 '12 at 15:55
  • Ansari, I added my new rewrite code as an edit to my original post. I think I got it correct. But verification/confirmation would be greatly appreciated! Thanks for all your help! – dkmojo May 31 '12 at 18:08
  • Just modified your code to fix a few syntax errors. But there's no better test than to actually try it out :) – Ansari May 31 '12 at 18:12
  • Awesome, thanks! I don't have direct access to the httpd.conf file so I am waiting on the host to do this for me. There is another query param I forgot about (&image=NUMBER) these don't matter anymore meaning they all should point to the new product detail URLs. I looked around but couldn't find an example of this. Could you point me to an example by chance? Thanks again – dkmojo May 31 '12 at 18:48
  • You're welcome. Just have your host put the RewriteMap definitions in httpd.conf and you can put the rewrite rules in .htaccess for easier debugging and modifying. If the image parameter occurs after the item parameter, then the rule you have up there will handle it (i.e. ignore it). – Ansari May 31 '12 at 18:51
  • OK, cool. I would of course need additional rules/cond for URLs that only contain 1 or 2 of the query params. So for those URLs with only the collection=1 param I need another rule, preceded by the URLs with both collection=1 and product=1 but not the item=1 param. Am I correct in this thinking? I appreciate all your answers and help. The Apache site doesn't give too many examples. – dkmojo May 31 '12 at 19:00
  • Yes it's possible to do it all in one rule but I wouldn't recommend it. Better to keep it simple. Yes you'll need multiple rules, just order them by how specific they are, the most specific goes first. By specific I mean higher number of parameters handled. – Ansari May 31 '12 at 19:05
  • Hi Ansari, I added an edit to my original post. Getting an odd rewrite. Not exactly sure why though. Any direction is greatly appreciated. – dkmojo Jun 01 '12 at 14:37
  • Yes the map isn't working - either it's not set up right, or it's not finding the map file, or the value isn't being found in the file, or the server hasn't been restarted after adding the maps to it. Also to get rid of the ?collection=1 at the end after rewriting add a question mark to the end of the rewriterule. – Ansari Jun 01 '12 at 15:48
  • Thanks. I do believe it is the maps file then. I don't have direct access to the httpd.conf file and I believe the hosting company has not restarted the server. I'll check with them again and see what the problem may be. – dkmojo Jun 01 '12 at 18:14