Search code examples
apache.htaccessmod-rewritemod-alias

Simple mod_rewrite RewriteRule for messy legacy url's


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?


Solution

  • 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]