简体   繁体   中英

Rewrite from url with querystring to folder

I want rewrite url like this:

/files/b9f8d0b5e35248579953755b3677a59b.png?w=400&h=100&mode=crop

To:

/files/400/100/crop/b9f8d0b5e35248579953755b3677a59b.png

My rule like:

.AddRewrite(@"^files/(.*)?w=(\d+)&h=(\d+)&mode=(.*)$", "files/$2/$3/$4/$1", true)

But it's not working, how can i fix it? Many thanks!

Your regular expression starts with the ^ which makes the pattern only match when it starts with files/ . Otherwise it looks pretty good. I've used [^?] as a character group that matches anything except the ? , and a similar character group for [^&] .

AddRewrite(@"/files/([^?]+)\?w=(\d+)&h=(\d+)&mode=([^&]+)", "/files/$2/$3/$4/$1", true)

^ Tested on https://www.regexplanet.com/share/index.html?share=yyyyye98k3r

You might want to consider what will happen if the query parameter order changes.

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