0

I am trying to use rewrite rules in IIS to route requests like this:

domain.com/users/some%20user%20name

to

domain.com/cms.asp?P=users/some user name

The issue I am seeing is that the rule is removing the spaces altogether. Here is the rule I use:

            <rule name="Rewrite Everything Else to cms.asp" stopProcessing="true">
                <match url="(.*)" />
                <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
                <action type="Rewrite" url="cms.asp?p={R:1}" logRewrittenUrl="true" />
            </rule>

What is causing the spaces to be removed during the rewrite and how can I prevent that?

GWR
  • 1,878
  • 4
  • 26
  • 44

1 Answers1

1

You can not have spaces in a URL. The http spec states

Spaces and control characters in URLs must be escaped for transmission in HTTP, as must other disallowed characters.

See here

If you would prefer something more readable, perhaps rewrite the rules encoding the spaces as underscores or dashes. If you have a know amount of spaces you can probably catch it using something like

<rule name="Rewrite Everything Else to cms.asp" stopProcessing="true">
            <match url="^(.*)%20(.*)%20(.*)%20(.*)" />
            <conditions logicalGrouping="MatchAll" trackAllCaptures="false"      />
            <action type="Rewrite" url="{R:1}-{R:2}-{R:3}-{R:4}" logRewrittenUrl="true" />
        </rule>

And then repeating the entry for :

^(.*)%20(.*)%20(.*)         replaced by:  {R:1}-{R:2}-{R:3}
^(.*)%20(.*)                replaced by:  {R:1}-{R:2}

Which will handle two and one space.

If you have an unknown number of spaces, you will probably need to implement an IIS rewrite module.

See here and here for more details

Community
  • 1
  • 1
swestner
  • 1,881
  • 15
  • 19