Score:0

Regex to remove certain patterns from urls

cn flag

So i've tried this multiple times but to no avail.

Basically, what I am trying to do is check if the URL has a certain language locale and remove it and do a 301 redirect to the parent url without the locale. For example, I have...

www.domain.com/mx/en-us/product/asset/23456768
www.domain.com/de/en-gb/product/asset/34565768
www.domain.com/ar/en-us/product/asset/34567788
www.domain.com/ar/en-us/affiliates

The pattern I would like to remove is any case of /en-us or /en-gb. So the new urls would look like...

www.domain.com/mx/product/asset/23456768
www.domain.com/de/product/asset/34565768
www.domain.com/ar/product/asset/34567788
www.domain.com/ar/affiliates

I have this, but its not working in NGINX

server {
    rewrite ^/en-us(.*)$ $1 last;
    rewrite ^/en-gb(.*)$ $1 last;
    return 301;
  }

Am I missing something? Should I not be using rewrite? Also can this be done in one line?

yagmoth555 avatar
cn flag
Hi ! for any edit please log to your account, and it will allow you to comment to people on your own post too. Thanks for your understanding !
Score:1
gr flag

If all your URIs should match the pattern you given (two letter prefix before the locale part):

rewrite "^(/\w{2})/en-(?:us|gb)(/.*)" $1$2 permanent;

(regex pattern should be quoted due to the curly braces usage)

If such a prefix length can vary:

rewrite ^(/\w+)/en-(?:us|gb)(/.*) $1$2 permanent;

To match the locale part everywhere:

rewrite ^(.*)/en-(?:us|gb)/(.*) $1/$2 permanent;
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.