Convincing IIS7 to accept URLs containing plusses (+)

Problem:

Old retiring IIS 5.0 web server has been accepting URLs containing plus (+) for spaces instead of %20 for like 74 years. People have the old URLs bookmarked and stuff so they’ll keep going to them. The content will still exist on the replacement IIS 7 web server. Wouldn’t it be nice to make it transparent?

(Not to mention “foo+bar.pdf” is sane, but “foo%20bar.pdf” reads “foo percent twenty bar”, awkwardly. )

The problem is that IIS7 by default considers naked plusses in the URL as scary and sends a 404.11, URL_DOUBLE_ESCAPED error. Even if you convince it the URL is OK, it no longer maps plus to space and finds a piece of content.

On the old IIS 5.0 server these URLs both work, serving up the document named “foo bar.pdf”:

http://server/foo+bar.pdf
http://server/foo%20bar.pdf

On the new IIS 7 (Windows 2008) server, the second URL works but the first one gives an error.

Solution:

I put this in my application’s web.config file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <security>
      <requestFiltering allowDoubleEscaping="True" />
    </security>
    <rewrite>
      <rules>
        <rule name="RewriteUserFriendlyURL1" stopProcessing="false">
          <match url="\+" />
          <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
          </conditions>
          <action type="Rewrite" url="{UrlDecode:{REQUEST_URI}}" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

the allowDoubleEscaping directive on line 5 solves the first part of the problem, allowing IIS to handle unescaped plusses. The rewrite rule below passes the requested URI through the UrlDecode function (line 15), which thankfully still remembers the time-tested convention of plus equaling space. Now both forms of the URL work.

(I didn’t really write the rewrite stanza, I just stumbled around with the rewrite URL editor in the IIS Manager until it worked)

Yay!