0
votes

Launching a new website for a new client. Their old site has about 50 products and unfortunately, the old product names do not match up to the new URL pattern

Old URL Examples:

example.com/products.aspx?category=Foo&product=SuperLongNoBreakProductNameIDDescription

example.com/products.aspx?category=Foo&product=ProductNameDescription&var1=1293.123

example.com/products.aspx?category=Bar&product=ProductCategoryProdNameRandomNumbers

(The old URL's are sometimes hitting 150+ characters.)

New URL's:

example.com/products/category/actual-product-name

There's no set, recognizable pattern to go from the old product name to the new one. There is for the category.

I've tried simple mod_alias Redirects, but understand that I need a RewriteRule instead. But I'm having problems. All I need is a 1-to-1 redirect for each of these 50 URL's. I thought I could do something like:

RewriteRule ^/products.aspx?category=Foo&product=ProductName
    /products/category/new-product-name/  [R=301,NC]

But that isn't working. I know this should be simple, but I am stuck. Any ideas?

3
For good form you should probably escape the period, e.g. products\.aspx. Also, see answer below about RewriteCond. Are there any other RewriteRules later in the .htaccess that could affect the rewritten URLs? You might want to use the L flag where you're doing the external redirect. - JMM
Thanks for the response JMM. No other rewrite rules. The rest of the site Iw as able to take care of with simple redirects. No external redirects, either. - the_zero
You don't clarify whether you have root config access and are doing these rewrites in your vhost config or alternatively using .htaccess based rules. Because if the latter is true then maps don't work and your regexps are wrong: no leading / on per dir regexps. - TerryE
@TerryE - sorry for being vague. This is being done in the vhost, not in .htaccess files. - the_zero

3 Answers

1
votes

Use the pattern below for the rest of your redirect urls. Note that you escape special characters e.g. ? , . and space by adding a \ in front of them

RewriteEngine on
RewriteBase /

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /products\.aspx\?category=Foo&product=SuperLongNoBreakProductNameIDDescription [NC]
RewriteRule ^ /products/category/new-product-name/  [R=301,NC]
0
votes

Have a look at the RewriteMap directive of mod_rewrite.

You can specify in a text file something like:

products.aspx?category=Foo&product=SuperLongNoBreakProductNameIDDescription /products/category/new-product-name

And in your httpd.conf

RewriteMap productmap /path/to/map/file.txt
RewriteRule ^(.*) ${productmap:$1} [R=301,NC]

Tip: If it's a permanent redirect you want, make sure you set an appropriate Cache-Control and Expires header to instruct browsers to cache the 301.

0
votes

You can try something like this:

RewriteEngine On

RewriteCond %{QUERY_STRING} ^category=Foo&product=ProductName$

RewriteRule ^products\.aspx$ /products/category/new-product-name/? [R=301,L]

Notes:

  1. In per-dir (.htaccess) context, the per-dir prefix is stripped, so you can't start the RewriteRule pattern with ^/.

  2. You have to use RewriteCond to match against the query string.

  3. As stated in another answer, a RewriteMap solution might be suited to this situation, if you have access to httpd.conf / the vhost definition for this site. I'm not sure how that works with query strings though.

  4. For something like this, it might be a better solution to rewrite all of these URLs to a server side script, and use the script to do the HTTP redirect for each URL.