28

Let's say a user tries to access a given image on my website using the following url: http://www.mywebsite.com/blog/image1.jpg?someParam=100

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

http://www.mywebsite.com/image1.jpg?someParam=100

2 Answers 2

35

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
  • This works for in the same server. How to make it work for the different domain? Commented May 6, 2013 at 9:56
  • 1
    Just include the new domain as part of the rewrite: rewrite ^/blog(/.*)$ https://blog.example.com$1 permanent; Commented Aug 29, 2017 at 18:24
4

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.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.