How to redirect old urls with capturing only the variable end and add multiple possible targets before


domain.com/product/product-link-xyz ...



My not working solution so far

location ~ ^/product/(.*) {
    rewrite ^(/product/.*)\..*$ $1/products/brand1/$2 break;
    rewrite ^(/product/.*)\..*$ $1/products/brand2/$2 break;

returns in every /product/product-xyz ends in 404 not found.

thanks in advance for your kind help

  • This article will be useful. You're basically going to have to use regular expressions and capture groups. It's fairly simple, now I've given you some direction you should be able to work it out yourself. Give it a go for a few hours, you'll probably learn something interesting. If you don't get anywhere reply to the comment and I'll have a look at it tomorrow. – Tim May 11 '17 at 21:23
  • Please don't put code inside comments, it's virtually unreadable. Edit your question to add it in a code block (4 spaces indented) and post a comment about the update. If it didn't work you have to work out if it triggered, and if it did why it didn't work. Regular expressions are a pain to debug, I use RegEx101 – Tim May 11 '17 at 21:58
  • You cannot have multiple targets for a single source URL in nginx. Either you have multiple source URLs, where you capture a part which is used to form multiple destination URLs. Or you have multiple source URLs which are forwarded to a single destination URL. If you want multiple targets for a single source URL, you need to implement the logic in your application. Furthermore, your example URLs and nginx configuration example are contradictory. – Tero Kilkanen May 12 '17 at 10:38
  • As you can see only some part of the source url is always the same. The end of the url changes (so this needs to be captured, forwarded) All these urls already exists and i need to redirect them to the new url structure. There is no way to implement this somewhere in my application logic. Its a simple wordpress blog btw and i don´t want to create a redirect for every single url. So really not possible? Anyone else maybe? – user3228274 May 12 '17 at 19:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.