Search code examples
regexurl-rewritingisapi-rewrite

Handling encoded characters in ISAPI - Regex


I have a url that contains a ~, and I need to handle the encoded version of this character %7E.

Currently, we have 2 rules to handle this case.

^/folder/([\w-]+)~(\d*).aspx$
^/folder/([\w-]+)%7E(\d*).aspx$

Can I combine this into one rule? I tried the following but it does not work:

^/folder/([\w-]+)[~|%7E](\d*).aspx$

Any help with this rule is appreciated.


Solution

  • Maybe you would like to try

    ^/folder/([\w-]+)(~|%7E)(\d*)\.aspx$
    

    or

    ^/folder/([\w-]+)(?:~|%7E)(\d*)\.aspx$
    

    to keep your capture groups the same if you regex engine supports this syntax.

    This […] constructs a character class, not a group of alternatives. So [~|%7E] means "either a literal ~, |, %, 7 or E.

    You have to use parens for that. And you should escape the . in \.aspx because . means "any character" in regexes.