douyi3632 2014-01-02 13:44
浏览 31
已采纳

NGINX重写规则,将部分URL传递给PHP - Wishlist Member

In Nginx, how can I select the bold part of the URL below? 2013 and 12 are relative to the date so the rule needs to be flexible to work future locations.

example.com/wp-content/uploads/2013/12/sometime.pdf

The end result should look something like: index.php?method=2013/12/sometime.pdf

Current rewrite rule

I have tried with the following but this will only return the last letter of the URL.

rewrite ^/wp-content/uploads/2013/12/sometime.pdf /index.php?method=$1

Thanks!

  • 写回答

3条回答 默认 最新

  • douyu0792 2014-01-02 14:57
    关注

    This expression will rewrite anything in the uploads folder, without a known extension, and passes the two parts of the URL as required:-

    rewrite ^/wp-content/uploads/(.*)\.(?!css|js|png|jpe?g|gif)(.*)$ /index.php?method=$1.$2
    

    $1, i.e. the first "(.*)" is anything between /wp-content/uploads/ and the . of the file extension.

    $2, i.e. the second "(.*)" is anything after the matched . of the file extension,

    Negative look aheads are the trick here: (?!.....)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?