This article focuses on using web.config files to redirect browsers via a 301, 302, or 307 status code. This article details steps required to redirect individual pages to another page or site. This article is the third in a series. Part one explains what http redirect status codes are, and provides several example web.config files. Part two shows examples of web.config files in action.

Purpose
HTTP response redirect status codes are used to redirect web requests for a web site, directory, or page to another location. The redirect could target another page or directory on the same domain, or a page or directory on another domain. Response redirect status codes have many uses, but they are most often used after redesigning a web site, changing domain names, or merging two or more web sites.

Compatibility
The httpRedirect section of web.config is compatible with IIS 7 (w2k8) and IIS 7.5 (w2k8 r2).

Web.config files are deeply integrated with IIS 7.x. The httpRedirect directives listed in this article will apply to all files and directories (php, jpg, png, htm, etc), not just asp.net files.

While some web.config sections sometimes require that the containing directory is set as an application, this isn't one of them. A simple web.config with a httpRedirect section may be placed in any directory, and the directory does NOT need to be set as an application.

Prerequisites

  • Windows 2008 Server, IIS 7 (w2k8) or IIS 7.5 (w2k8 r2)
  • IIS sub feature: HTTP Redirection (not installed by default with IIS)
  • HTTP Redirection delegation set to: Read/Write

I already know
Yes, I know that we can redirect individual pages by placing the following into the head of the page
 <META HTTP-EQUIV=Refresh CONTENT="0; URL=http://www.foo.com/page.htm" />, but that's not what we are talking about. Here, we are using web.config to do the same thing. One advantage of using web.config for the redirect, is control over the status code. With web.config, we can set the status code to 301, 302, or 307.


Example
In the following example, the "pages" directory contains page1.htm, page2.htm, page3.htm, and page4.htm. The web.config shown below will do the following

 

<?xml version="1.0"?>
<configuration>
    <location path="page1.htm">
        <system.webServer>
            <httpRedirect enabled="true" destination="http://www.victor-ratajczyk.com/examples/webconfig/redir/newpages/newpage.htm" httpResponseStatus="Permanent" />
        </system.webServer>
    </location>
    <location path="page2.htm">
        <system.webServer>
            <httpRedirect enabled="true" destination="http://www.google.com" httpResponseStatus="Permanent" />
        </system.webServer>
    </location>
    <location path="page3.htm">
        <system.webServer>
            <httpRedirect enabled="true" destination="http://news.yahoo.com/science/" httpResponseStatus="Permanent" />
        </system.webServer>
    </location>
    <location path="page5.htm">
        <system.webServer>
            <httpRedirect enabled="true" destination="http://www.google.com" httpResponseStatus="Permanent" />
        </system.webServer>
    </location>
</configuration>