将URL重写为特定查询部分Nginx

问题描述 投票:0回答:1

[我遇到了以下情况,我想重写以下URL,并将我的第一个URL参数作为data参数发送,其余作为query参数发送

www.example.com/anything.jpg      //Skip - Do Nothing because its a file from root
www.example.com/anything          //anything == ?data=$1
www.example.com/something/b       //something == ?data=$1    b == &query=$2
www.example.com/certainly/b/c     //certainly == ?data=$1    b/c == &query=$2
www.example.com/anything/b/c.jpg  //anything ==  ?data=$1    b/c.jpg == &query=$2
www.example.com/but/b/c/d         //but ==       ?data=$1    b/c/d == &query=$2

因此我的第一个URL参数将始终为data,并且无论其具有什么,都将保持为query。我在这里htaccess redirect only if it has value after forward slash找到了这个问题,并在某种程度上得到了它,例如

rewrite ^/(.*)/(.+)$ /handle.php?data=$1&query=$2 last;

它有一些问题,因为它重写为以下内容

www.example.com/anything.jpg      //Results in 404 Not Found
www.example.com/anything          //Results in 404 Not Found
www.example.com/something/b       //Working good
www.example.com/certainly/b/c     //data=certainly/b    query=c
www.example.com/anything/b/c.jpg  //data=certainly/b    query=c.jpg
www.example.com/but/b/c/d         //data=but/b/c        query=d

我尝试在rewrite ^/(.*)/(.+)$ /handle.php?data=$1&query=$2 last;中使用不同的变体,但没有任何效果

php apache nginx url url-rewriting
1个回答
0
投票

为了证明正则表达式匹配的结果,我将用preg_replace()进行演示,但是该模式应无缝转换为您的脚本。

© www.soinside.com 2019 - 2024. All rights reserved.