Sign up ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I am having an issue with Nginx Rewrites

Currently my rule is as seen below

rewrite ^/i/(.*?)$ /i/$1.php last;

Basically what I want to do is redirect all .png files to .php within the /i directory. However, it seems that the $ has to be at the end so that I can not do

rewrite ^/i/(.*?)$.png /i/$1.php last;

Does anyone have any solutions?

Thanks Ben

share|improve this question
Please post the entire server block of your nginx configuration. – Michael Hampton Aug 3 '12 at 20:51
Here we go – Ben Aug 3 '12 at 20:54

2 Answers 2

Requests for .png files are being handled by your location ~* \.(js|css|png|jpg|jpeg|gif|ico)$. Just stop that from handling png files and add a new location that only handles them:

server {
  location ~* \.(js|css|jpg|jpeg|gif|ico)$ {
    # the same stuff you already had in here

  location ~* ^(?<basename>.*)\.png$ {
    rewrite ^ $basename.php last;

  # your other locations
share|improve this answer
Thankyou! It worked wonders – Ben Aug 4 '12 at 10:08
mark it as accepted @Ben. thanks – peteroak May 30 at 22:07

Oh, now I see the problem.

Your rewrite rule looks like this:

rewrite ^/i/(.*?)$ /i/$1.php last;

So this would rewrite /i/cute.png to /i/cute.png.php. It probably doesn't exist.

You said you simply wanted to change .png to .php, so try something like this:

rewrite ^/i/(.*?).png$ /i/$1.php last;
share|improve this answer
Nope :( Still not working :@ – Ben Aug 3 '12 at 21:56

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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