Score:0

301 redirect url based off of a category in the url

cn flag

Does anyone know how to redirect a url that contains a parent category of a product? Basically I want to redirect a ton of products to a simple landing page that I created, so instead of making redirects for each product, I want to target the parent category (manufacturer) and redirect it to my one landing page.

So like these:

https://example.com/product/baader/baader-600/baader-600-belts/belt-rubber/
https://example.com/product/baader/baader-600/baader-600-belts/belt-urethane/
https://example.com/product/baader/baader-600/baader-600-parts/cover/
https://example.com/product/baader/baader-600/baader-600-parts/washer/

Would all redirect to this one landing page:

https://example.com/replacement-parts/baader/baader-600/

So I guess you would target anything that has "product/baader/baader-600/" and then ditch the last part of the url (/baader-600-belts/belt-rubber/) and redirect it to "/replacement-parts/baader/baader-600/" I have no idea how to make sus a RewriteRule.

Score:0
sv flag

Welcome to ServerFault.

We don't need a complex rewrites to achieve it. The following would work...

location /product/baader/baader-600/ { return 302 /replacement-parts/baader/baader-600/; }

Rewrite equivalent...

rewrite ^/product/baader/baader-600/ http://example.com/replacement-parts/baader/baader-600/;

If you require 301 redirect, then here are the equivalents...

location /product/baader/baader-600/ { return 301 /replacement-parts/baader/baader-600/; }
rewrite ^/product/baader/baader-600/ /replacement-parts/baader/baader-600/ permanent;
Score:0
ru flag

Assuming the manufacturer must be dynamic, then you could add such a rule.

location ~ /product/(.+) {
    rewrite ^.*(\/product\/)([\w|-]*)\/(.*)\/.*$ /replacement-parts/$2/$3 permanent;
}

Here $2 is a second match group for manufacturer, and $3 contains product part. 

 
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.