Nginx rewrite rule to remove path node

Let's say a user tries to access a given image on my website using the following url:

i need a rewrite rule to this, removing the 'blog' node from the path:

2 Answers

Try this one:

location /blog { rewrite ^/blog(/.*)$ $1 last;
}

If you need this for more than one site you can't just put it higher in hierarchy because "location" clause can't be specified globally, only for specific site. If you need to add this clause for two sites or more you can put it another config file and then just "include" it in each site that needs this redirection.

2

Depending where you define the rewrite directive you have two ways to implement it:

A. In the server context

server { ... rewrite ^/blog(/.*)$ $1 last; return 403; ...
}

B. In the location context

location /blog { rewrite ^/blog(/.*)$ $1 break;
}

Teo, why did you change the flag to break?* Because, if this directive is put inside of a location context, the last flag might make nginx to run 10 cycles and return the 500 error.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like