Anh Tú
Anh Tú

Reputation: 636

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!

Upvotes: 0

Views: 44

Answers (1)

Sean
Sean

Reputation: 1289

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.

Upvotes: 1

Related Questions