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?
You can try something like this:
Notes:
In per-dir (.htaccess) context, the per-dir prefix is stripped, so you can't start the
RewriteRule
pattern with^/
.You have to use
RewriteCond
to match against the query string.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.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.
Have a look at the
RewriteMap
directive ofmod_rewrite
.You can specify in a text file something like:
And in your
httpd.conf
Tip: If it's a permanent redirect you want, make sure you set an appropriate
Cache-Control
andExpires
header to instruct browsers to cache the 301.Use the pattern below for the rest of your redirect urls. Note that you escape special characters e.g.
?
,.
and spaceby adding a
\
in front of them