Star InactiveStar InactiveStar InactiveStar InactiveStar Inactive
 

Using web.config - Redirecting browsers with 301, 302, and 307 status codes on IIS 7 and IIS 7.5
The web.config file allows you to redirect browsers from one site, directory or page, to another site, directory, or page. Response redirect status codes have many uses, but they are most often used after remodeling or changing the layout of a web site.

While some web.config sections 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.

What are http response redirects?
HTTP response redirect status codes are used to redirect web requests for a web site or directory, to another location. The redirect could target another page or directory on the same domain, a different base domain, or to a page or directory on another domain.

Web.config based redirects

  • <httpRedirect enabled="true" destination="http://foo.com" httpResponseStatus="Permanent" /> <!-- 301 permanent redirect -->
  • <httpRedirect enabled="true" destination="http://foo.com" httpResponseStatus="Found" /> <!-- 302 found redirect -->
  • <httpRedirect enabled="true" destination="http://foo.com" httpResponseStatus="Temporary" /> <!-- 307 temporary redirect -->
  • <httpRedirect enabled="true" destination="http://www.foo.com/foo.htm" exactDestination="true" /> <!-- 302 (found) redirect, to a specific page or directory -->
Redirect status codes
301 permanent redirect - Moved permanently
The requested resource has been assigned a new permanent URI and any future references to this resource should use the new URL.
  • Permanently redirect a site or subdirectory to another domain.
  • <httpRedirect enabled="true" destination="http://foonew.com" httpResponseStatus="Permanent" />
  • Permanently redirect a site or subdirectory to a subdirectory on the same domain.
  • <httpRedirect enabled="true" destination="http://foo.com/newdir" httpResponseStatus="Permanent" />
  • Permanently redirect a site or subdirectory to a specific page.
  • <httpRedirect enabled="true" destination="http://foo.com/foo.htm" exactDestination="true" httpResponseStatus="Permanent" />
302 found redirect
The requested resource resides temporarily under a different URL. Since the redirection might be altered on occasion, the client should continue to use the old URL for future requests
  • Redirect a site or subdirectory to a specific page.
  • <httpRedirect enabled="true" destination="http://foo.com/overloaded.txt" exactDestination="true" httpResponseStatus="Found" />
307 temporary redirect - Temporary redirect
The requested resource resides temporarily under a different URL. Since the redirection might be altered on occasion, the client should continue to use the old URL for future requests
  • Temporarily redirect a site or subdirectory to a specific page.
  • <httpRedirect enabled="true" destination="http://foo.com/overloaded.txt" exactDestination="true" httpResponseStatus="Temporary" />

Using HTTP redirects
  • Use a text editor to create a file named web.config
  • Save the web.config file with the appropriate content
  • Place the web.config file in the directory that you wish to redirect. If you wish to redirect the entire site, place the web.config in the web root. If you wish to redirect foo.com/google to google.com, place the web.config in the /google directory of the web root

Detailed web.config content
Let's redirect foo.com/olddir to somewhere else.
  • If there isn't an existing web.config in the "olddir" directory, your new web.config should look something like this
    <?xml version="1.0"?>
      <configuration>
        <system.webServer>
          <httpRedirect enabled="true" destination="http://foo.com/newdir" httpResponseStatus="Permanent" />
        </system.webServer>
      </configuration>
  • If there is an existing web config, without a <system.webServer> section... Your new web.config should look like this
    <?xml version="1.0"?>
      <configuration>
        <system.web>
          ..existing text..
          ..existing text..
        </system.web>
        <system.webServer>
          <httpRedirect enabled="true" destination="http://foo.com/newdir" httpResponseStatus="Permanent" />
        </system.webServer>
      </configuration>
    
  • If your existing web.config already has a <system.webServer> section, just add the <httpRedirect> section
    <configuration>
      <system.web>
        .. existing text ..
        .. existing text ..
      </system.web>
      <system.webServer>
        <security>
          <ipSecurity allowUnlisted="true">
            <add ipAddress="83.116.19.53"/>
            <add ipAddress="83.116.119.0" subnetMask="255.255.255.0"/>
          </ipSecurity>
        </security>
        <httpRedirect enabled="true" destination="http://foo.com/newdir" httpResponseStatus="Permanent" />
        <modules runAllManagedModulesForAllRequests="true"/>
      </system.webServer> 
    </configuration>

Redirecting specific pages
Maybe we want to redirect a few specific pages within the foo.com/searchengines directory, to various targets. We would place the following web.config in the foo.com/searchengines directory.
<?xml version="1.0"?>
  <configuration>
    <location path="bing.htm">
      <system.webServer>
        <httpRedirect enabled="true" destination="http://bing.com" httpResponseStatus="Permanent" />
      </system.webServer>
    </location>
    <location path="google.htm">
      <system.webServer>
        <httpRedirect enabled="true" destination="http://google.com" httpResponseStatus="Permanent" />
      </system.webServer>
    </location>
    <location path="yahoo.htm">
      <system.webServer>
        <httpRedirect enabled="true" destination="http://yahoo.com" httpResponseStatus="Permanent" />
      </system.webServer>
    </location>
  </configuration>