
February 7, 2012 18:23 by
Victor Ratajczyk |
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 an entire site or directory. You are currently on part one of this article. Part two shows the examples on this page in action. A third article will focus on redirecting individual pages.
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
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
Example web.config redirects
Example IP address restrictions. Comments are enclosed in <!-- --> and are not required
<?xml version="1.0"?>
<configuration>
<system.webServer>
<!-- 301 permanent redirect -->
<httpRedirect enabled="true" destination="http://foo.com" httpResponseStatus="Permanent" />
<!-- 302 found redirect -->
<httpRedirect enabled="true" destination="http://foo.com" httpResponseStatus="Found" />
<!-- 302 found redirect, to a specific page or directory -->
<httpRedirect enabled="true" destination="http://www.foo.com/foo.htm" exactDestination="true" />
<!-- 307 temporary redirect -->
<httpRedirect enabled="true" destination="http://foo.com" httpResponseStatus="Temporary" />
</system.webServer>
</configuration>
Detailed web.config content
Let's redirect http://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>