1

I have a couple thousand URL's that look like the following:

https://www.example.com/supplier-shop/u-23452345/s-p/
https://www.example.com/supplier-shop/u-1714128138
https://www.example.com/supplier-shop/u-436877957/s-p
https://www.example.com/supplier-shop/u-32452345
https://www.example.com/supplier-shop/u-2345245664
https://www.example.com/supplier-shop/u-23452345/

This is from my legacy website but our new URL structure looks like the following:

www.example.com/seller/xxxxxxxx

I know how to rewrite single URL's but how would I do a catch all case for all of my id's?

1
+50

Here's your answer:

location ~ ^/supplier-shop/(?<id>(.*)) {
    rewrite ^ http://$server_name/seller/$id permanent;
}

Works as well for n level directories as well. Example:

> GET /supplier-shop/asdasd/asdasd HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.47.0
> Accept: */*
>
< HTTP/1.1 301 Moved Permanently
< Server: nginx/1.10.3 (Ubuntu)
< Date: Thu, 28 Jun 2018 00:10:02 GMT
< Content-Type: text/html
< Content-Length: 194
< Connection: keep-alive
< Location: http://www.example.com/seller/asdasd/asdasd
1

I guess with a rewrite like the following you may achieve what you are looking for

location ~ ^/supplier-shop/(.+) {
    rewrite ^ http://$server_name/seller/$1 permanent;
}

Guess the http://$server_name can be omitted, but honestly I don't have an nginx to try what I'm suggesting right now, so better try it and see the results.

Basically the it makes is replacing the "supplier-shop" with the "seller" and taking all that follows and putting into place.

If it's not what you are trying to achieve, let us know

  • All this is doing is stripping everything after /supplier-shop/ and replacing it with seller – NooBskie Jun 22 '18 at 0:49

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.