简体   繁体   中英

Nginx rewrite partial url

I have the following url:

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

The numbers in between (10103) can change as can the image names. I've tried the following nginx rewrite rule:

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. 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 . As you posted in the comment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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