Simple mod_rewrite RewriteRule for messy legacy url's

帅比萌擦擦* 提交于 2019-12-12 02:47:01

问题


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?


回答1:


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]



回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/8975621/simple-mod-rewrite-rewriterule-for-messy-legacy-urls

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!