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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器