简体   繁体   English

Nginx重写部分网址

[英]Nginx rewrite partial url

I have the following url: 我有以下网址:

http://www.example.com/img/products/10103/f/10103-s-1.jpg http://www.example.com/img/products/10103/f/10103-s-1.jpg

And would want to rewrite it to: 并希望将其重写为:

http://www.example.com/img/products/10103/10103-s-1.jpg http://www.example.com/img/products/10103/10103-s-1.jpg

The numbers in between (10103) can change as can the image names. (10103)之间的数字可以更改,图像名称也可以更改。 I've tried the following nginx rewrite rule: 我尝试了以下nginx重写规则:

rewrite ^/img/products/(.+)$/f /img/products/$1 last;

Why does it now work? 为什么现在可以使用?

The current rewrite rule is matching on 当前的重写规则符合

^/img/products/(.+)$/f

Which means URL path starts with /img/products/ then anything follows and the URL ends, and then /f follows. 这意味着URL路径以/img/products/开头,然后跟随所有内容,URL结束,然后跟随/f This can never match a single line as you're using the dollar sign which indicates a line end, and then have characters after it. 当您使用指示行尾的美元符号时,它永远不会匹配任何一行,然后在其后有字符。

If you want to match 如果你想匹配

/img/products/10103/f/10103-s-1.jpg

You'll have to change it to ^/img/products/(.+)$ , but as you want to extract the two id-groups you're looking for ^/img/products/(.+)/f/(.+)$ , then replace it with /img/products/$1/$2 . 您必须将其更改为^/img/products/(.+)$ ,但是当您要提取两个id组时,您正在寻找^/img/products/(.+)/f/(.+)$ ,然后将其替换为/img/products/$1/$2 As you posted in the comment. 正如您在评论中发布的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM