logo logo

IIS 7 URL Rewrite (HTTP Redirect) to root site

Recently, I had a project to setup a product which had instructions in the scenario of a dedicated web server. Since I wanted to install this on an existing web server, I created a virtual host for the site, and installed it. I went through the process of creating the virtual host, binding it to a hostname, which coincided with a DNS entry.
The problem product was setup in such a way that the actual root of the application resides in a subfolder of the root of the site, since this was a vhost, and I did not want people to type in http://mysite.domain.com/folder1/folder2 to get to the index of the site, I needed to create an HTTP redirect that goes http://mysite.domain.com/ to http://mysite.domain.com/folder1/folder2.

The first thought was to use the IIS 7 built-in HTTP Redirect module. In theory, this sounds like a very viable solution.
So, I tried it, and set it up this way:

IIS 7 HTTP Redirect

You will be surprised to see that this scenario will cause a loop, and will never actually load the page. When looking at the logs, it will look something like this:
http://mysite.domain.com/folder1/folder2/folder1/folder2/folder1/folder2/folder1/folder2 etc ….

(Chrome will return a 301 ERROR:: TOO MANY REDIRECTS)

In order to resolve this problem, I had to write a URL rewrite that would actually process the URL correctly.

Here’s the correct way to write do this URL rewrite for it to work. There are 2 ways you can do this, either from the GUI, or by editing the web.config file in the root of the site. I’ll show you both methods.

The text file method:

If it doesn’t already exist, go to the root of your site: (http://mysite.domain.com) , and create a file called web.config, then put the following in it:

<configuration>
      <system.webServer>
    <rewrite>
        <rules>
            <rule name="Root Hit Redirect" stopProcessing="true">
                <match url="^$" />
                <action type="Rewrite" url="/psp/app" /&>
            </rule>
        </rules>
    </rewrite>
  </system.webServer>
</configuration>

The GUI method:

In the GUI, go to URL Rewrite, then right-click in the “Inbound Rules”, and create a new rule, and choose a “Blank Rule” template from the Inbound Rules.
Now, fill in the rules as follows:

URL Rewrite Inbound Rule

Once this rule is in place. Reload the page, and the redirect should happen as expected.

 

Print Friendly

Related posts:

bottom
0 comments
bottom
Subscribe By Email for Updates.