简体   繁体   中英

Using htaccess to redirect query urls

I need to redirect a multiple urls within a few different categories using htaccess. The current structure is;

.com/category/subcat/product.html?querystring=123

And I need all versions of the querystring URL to redirect to;

.com/product.html

I don't mind writing a few lines for multiple categories but there are 1000's of variations of the querystring and cannot write all of them out.

Anyone have any ideas??

Try adding this to the htaccess file in your document root:

RewriteEngine On
RewriteCond %{QUERY_STRING} (^|&)querystring=123(&|$)
RewriteRule ^category/subcat/product\.html$ /product.html? [L,R=301]

As for the variations, if it's just a matter of a different numeric ID, then you can use:

RewriteCond %{QUERY_STRING} (^|&)jewelry_(metal|carat|colour|clarity)=[0-9]+(&|$)

If the "product" part is supposed to be variable, then replace the line with the rule with:

RewriteRule ^category/subcat/(.+)\.html$ /$1.html? [L,R=301]

If "category" and "subcat" can be anything then:

RewriteRule ^[^/]+/[^/]+/(.+)\.html$ /$1.html? [L,R=301]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM