Nginx Mod重写 – 将* .png重写为* .php

我在Nginx重写中遇到问题

目前我的规则如下所示

重写^ / i /(.*?)$ /i/$1.php最后;

基本上我想要做的是将所有.png文件redirect到/ i目录中的.php文件。 但是,看来这个$必须是最后的,所以我不能这样做

重写^ / i /(.*?)$。png /i/$1.php最后;

有没有人有任何解决scheme?

谢谢Ben

请求.png文件正在由您的location ~* \.(js|css|png|jpg|jpeg|gif|ico)$处理location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ 。 只要停止处理PNG文件,并添加一个新的位置,只处理它们:

 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 } 

哦,现在我看到了这个问题。

您的重写规则如下所示:

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

所以这会将/i/cute.png重写为/i/cute.png.php 。 它可能不存在。

你说你只是想把.png 改成 .php ,所以试试这样:

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