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条)

报告相同问题?

悬赏问题

  • ¥15 word样式右侧翻页键消失
  • ¥15 springboot+vue 集成keycloak sso到阿里云
  • ¥15 win7系统进入桌面过一秒后突然黑屏
  • ¥30 backtrader对于期货交易的现金和资产计算的问题
  • ¥15 求C# .net4.8小报表工具
  • ¥15 安装虚拟机时出现问题
  • ¥15 Selenium+docker Chrome不能运行
  • ¥15 mac电脑,安装charles后无法正常抓包
  • ¥18 visio打开文件一直显示文件未找到
  • ¥15 请教一下,openwrt如何让同一usb储存设备拔插后设备符号不变?