4

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

Ben
  • 41
  • 1
  • 2

2 Answers2

4

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
}
kolbyjack
  • 7,854
  • 2
  • 34
  • 29
0

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;
Michael Hampton
  • 237,123
  • 42
  • 477
  • 940