简体   繁体   中英

URL rewrite double appending question mark

I have the two rules:

<rewrite url="^/de(/.+)(\?(.+))?$" to="$1?lang=de&amp;$3" />
<rewrite url="^/terms-and-conditions(\?(.+))?$" to="~/Pages/Terms.aspx?$2" processing="stop"/>

This should make:

/terms-and-conditions
/de/terms-and-conditions

All point to the same page ( Terms.aspx )

This works in all test cases, except for a URL starting with /de and has a querystring.

/de/terms-and-conditions?x=y

Gives the querystring:

?x=y?lang=de&

How can I fix this rule to have an ampersand and not a second question mark?

I can get it to work by breaking it into two rules:

<rewrite url="^/de(/.+)\?(.*)$" to="$1?lang=de&amp;$2" />
<rewrite url="^/de(/.+)$" to="$1?lang=de" />

But for simplicities sake I'd like this in a single rule!

You can use the following regex:

^\/de(\/[^?]+)(\?(.+))?$

It words because (\\/[^?]+) (matching / and then 1 or more characters other then / ) restricts the regex to match just 1 subpart between / s, and (\\?(.+))? matches optionally (due to the final ? ) a literal ? and then any number of characters other than a newline.

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