user704988
user704988

Reputation: 436

IIS Redirect Regex doesn't work with a space - %20 in URL

So I have one simple problem but somehow doesnt seem to work. I have one URL http://www.domain.com/%20#axzz2ZX4J0KAS which I want to redirect to http://www.domain.com/page-name.htm. I have tried so many combinations in IIS URL Rewrite/web.config and they all seem to work inside test pattern dialog but none works in browsers.

1.

<rule name="Redirect%20InHomePage" enabled="true" stopProcessing="true">
    <match url="^(.+)domain\.com/(\s|%20)(.+)" ignoreCase="true" />
    <action type="Redirect" url="http://www.domain.com/page-name.htm" />
</rule>

2.

   <match url="(.+)/%20(.+)" ignoreCase="true" />

3.

    <match url="(.+)domain.com/ (.+)" ignoreCase="true" />

4.

   <match url="(.+)domain.com/(\s|%20)(.+)" ignoreCase="true" />

As you can see I tried all of above patterns, they all work fine in Test Pattern dialog but when i browse URL, it always converts %20 to space and rule doesn't work for redirect.

Please help me for this simple yet unsolved problem, if anyone knows what am I missing.

Upvotes: 3

Views: 4001

Answers (2)

emjohn
emjohn

Reputation: 69

Don't include the domain name in your match url.

If you want to handle either having the tracking code or not in your url then you probably want to use something like this:

<rule name="RedirectSpaceInHomePage" stopProcessing="true">
  <match url="^\s(#\.*)?$" />
  <action type="Redirect" url="page-name.htm" />
</rule>

Upvotes: 0

lathomas64
lathomas64

Reputation: 1642

I was having a similar issue and got it to work by typing spaces " " instead of %20 for my rules.

So here you may want to try [ ] for your space.

https://i.sstatic.net/N1Tmh.jpg

Upvotes: 4

Related Questions