IIS rewrite module - Encoded space at end of URL, looking for a rewrite rule to redirect the page

1.6k Views Asked by At

I have a pesky little problem. I am using MVC 3 with rewrite module. There is a link to my website from another website linking to mysite as:

hxxp:www.mysite.com/%20

i.e. the other site ads %20 (encoded space character) to the end of their url.

Now I need to redirect this link to the main site like so:

hxxp://www.mysite.com/

as otherwise it gives a 404 not found error.

Now the issue I have tried different rewrite rules but nothing seems to redirect the website e.g.

<rule name="redirect space" stopProcessing="true">
          <match url="^hxxp://www.mysite.com/%20" />
          <action type="Redirect" url="hxxp://www.mysite.com/" redirectType="Permanent" />
        </rule>

and many other combinations, but nothing seems to fix it and it gives 404 errors in every case.

1

There are 1 best solutions below

0
On

Try to add some variants for the space: encoded one, decoded, and a space character, like this:

<rule name="redirect space" stopProcessing="true">
  <match url="^hxxp://www.mysite.com/((%20)|\ |\s)+" />
  <action type="Redirect" url="hxxp://www.mysite.com/" redirectType="Permanent" />
</rule>

More information can be found here:

If this still isn't working, you have to write your own rewite module. You can see here a bit more details:

Replace %20 in Url with - in C#.net web.config