Spaces in IIS Url Rewrite rules

Over the past few weeks, I've been finding Microsoft's Url Rewrite add-on to IIS really useful for rewriting and redirecting Urls from our old Classic ASP CMS to our shiny new Umbraco CMS (see my previous blog post which explains why Url Rewrite is a better solution to Umbraco's built in Url rewriting tool).

One problem we came across was matching Urls where the querystring element contained a space.

Since I couldn't find a working solution after Googling for some time, I thought I'd share it here.

The browser encodes space characters found in the querystring to "%20".  For example:

www.mysite.com/?my test

... will be received by IIS as ...

www.mysite.com/?my%20test

So you need to match against my%20test.  However, there is a second gotcha, in that the percent character needs to be escaped in the rewrite rule.  So rather than writing %20, you must write \%20 - note the "\" character before %20.

In this particular case, I would be using this condition to match "my test":

<add input="{QUERY_STRING}" pattern="^my\%20test$" />

Incidentally, it seems that you don't need to match %20 when dealing with the Url itself, as IIS will already have decoded that element of the page address for you - its only the querystring where you will notice this behaviour.

Now everything is working fine again for us (for the time being) and we now have 1000+ rules.

Comments

  1. Hey Steve.
    I like your posts and I have been having some of the same issues.
    One thing I did notice however was that you wrote 1000+ rules.
    Holy hell that is a lot of rules.

    I spent a good amount of time creating some good redirect rules for our company and I figure you might want to take a look at it aswell, if you like ofcourse.
    I have put them on the official nuget repository.
    http://www.nuget.org/packages/RedirectRules/
    If you want to know more, feel free to contact me. You should be able to contact me through nuget.org. :)

    -Niels Ellegaard

    ReplyDelete
  2. Hello,

    I have a URL like this:
    mydomain.com/dvd%20player

    How do I write the complete rules and conditions, in the web.config file, so that I can rewrite the URL to this:

    mydomain.com/Default_Desktop.asp?thesearch=dvd%20player

    or rewrite the same URL to this:

    mydomain.com/Default_Desktop.asp?thesearch=dvd-player

    Thanks for your time on this...

    ReplyDelete
  3. I tried using this trick but its still not working for me, \s, [ ], \%20, none of them seem to work.

    ReplyDelete
    Replies
    1. I got this working by putting into conditions instead of the match url.







      Delete
    2. Oh... in conditions you can just use a " " (space), no escaping is required!

      Delete
  4. It's been a while since I looked at this, but I seem to remember its only the querystring that gets encoded with space becoming %20. So if you're adding a match rule for the Url itself, you only need to use the space character (and not %20).

    ReplyDelete
  5. I was unable to get anything to work. Any additional help you can provide would be well appreciated.

    ReplyDelete

Post a Comment