【问题标题】:NGINX Rewrite rule, passing part of the URL to PHP - Wishlist MemberNGINX 重写规则,将部分 URL 传递给 PHP - Wishlist Member
【发布时间】:2014-10-21 17:02:57
【问题描述】:

在 Nginx 中,如何选择下面 URL 的粗体部分? 2013 年和 12 年是相对于日期的,因此该规则需要灵活地适用于未来的位置。

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

最终结果应该类似于:index.php?method=2013/12/sometime.pdf

当前重写规则

我尝试了以下方法,但这只会返回 URL 的最后一个字母。

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

谢谢!

【问题讨论】:

  • 只需给出一个带有 index.php 的示例 URL。
  • 对不起,简森,我想我不明白你想要什么。
  • 您实际上想要实现什么?就像抓取 '2013/12/sometime.pdf' 并附加到 index.php 中?
  • 我需要一个规则来重定向上传文件夹中的任何内容。将 2013/12 和文件名传递给 php 方法。最终结果将类似于:index.php?method=2013/12/sometime.pdf

标签: php wordpress nginx


【解决方案1】:

此表达式将重写上传文件夹中的任何内容,无需已知扩展名,并根据需要传递 URL 的两个部分:-

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

$1,即第一个“(.*)”是 /wp-content/uploads/ 和 .文件扩展名。

$2,即第二个“(.*)”是匹配后的任何内容。文件扩展名,

消极的前瞻是这里的诀窍:(?!.....)

【讨论】:

  • 对不起,达伦,运气不好。
  • 重写 ^/wp-content/uploads/([0-9]{4})/([0-9]{2})/(.*)$ /index.php?method =$1 我认为这样的事情可能会奏效。但是 nginx 说这条线没有正确终止。
  • 小改动,将重定向标志添加到重写规则中:rewrite ^/wp-content/uploads/(.*)\.(?!css|js|png|jpe?g|gif)(.*)$ /index.php?method=$1.$2 redirect;
【解决方案2】:

如果你对 URL 没有任何细节,你可以做一个普通的位置块,用一点点正则表达式

location ^~ /wp-content/uploads/(.*) {
  try_files /index.php?method=$1 =404;
}

【讨论】:

  • 感谢 Mohammad,Darren 有更多信息可以解决这个问题。
【解决方案3】:

要在 nginx 中使用 wordpress 中的愿望清单成员插件,请添加以下规则以通过 wlmfile 方法解析文件并应用正确的权限。

location / {
    index index.php index.html index.htm;
    rewrite ^/wp-content/uploads/(.*)\.(?!css|js|png|jpe?g|gif)(.*)$ /index.php?wlmfile=$1.$2 redirect;
    try_files $uri $uri/ /index.php?q=$uri&$args;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-20
    • 1970-01-01
    • 2017-03-13
    • 1970-01-01
    相关资源
    最近更新 更多