Search code examples
nginxurl-rewritingnginx-locationwinginx

NGINX url rewrite - remove /web/guest/


I need to remove /web/guest/ from all urls on a portal via nginx.

Currently urls looks like this:

www.mywebsite.com/en/web/guest/blog-information
www.mywebsite.com/en/web/guest/something-else/information2
www.mywebsite.com/en/web/guest/blog-information3
and so on....

Should be:

www.mywebsite.com/en/blog-information
www.mywebsite.com/en/something-else/information2
www.mywebsite.com/en/blog-information3
and so on....

What should I add in nginx.conf in order to make this change work ?


Solution

  • This can be done with nginx rewrite, try add a rule like this:

    rewrite ^(.*)/web/guest/(.*)$ $1/$2 permanent;

    This will remove the last /web/guest/ in your uri, you can write a more specific rewrite rule depending on your situation.

    The last parameter given above is an optional flag, permanent is for 301 redirection and redirect is for 302, there are also other options so you'd better read the docs for more detailed information.