Nginx Mod Rewrite – Rewrite *.png to *.php

Ben asked:

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

My answer:


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;

View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.