IIS7 and 301 Permanent Redirects
Lets say your launching a new site to replace an old one. You want to make sure that peoples bookmarks and search engine information does not return 404 page not found. The typical way of doing this is to create a 301 permanent redirect on the old file. In IIS6 this was a complete pain. You had to click on every file in the IIS6 manager (before deleting the file) and setup a redirect to the new page. Time consuming and not very good IMO.
IIS7, and its integration with the web.config makes this process so much easier. Look at the example below for an example of how to specify 301 redirects in your web.config file:
<location path="ComputerLab.asp"> <system.webServer> <httpRedirect enabled="true" destination="Labs/" httpResponseStatus="Permanent" /> </system.webServer> </location> <location path="EmailSpamSolution.asp"> <system.webServer> <httpRedirect enabled="true" destination="Support/Spam.aspx" httpResponseStatus="Permanent" /> </system.webServer> </location> <location path="MicrosoftOffice2007.asp"> <system.webServer> <httpRedirect enabled="true" destination="Training/Help/Office2007.aspx" httpResponseStatus="Permanent" /> </system.webServer> </location>
Note: these additions go directly inside of <configuration> and NOT inside of <system.web> or <system.webServer>















2 Comments, Comment or Ping
Josh
Hi,
Great article, it is helpful in building out our tools. Let me run a slightly different scenerio by you. What if I have a domain, such as http://www.site.com that i want to redirect to http://www.realsite.com/folder/page.aspx. In this example, would i add the 2nd domain as a binding and then what would the location node look like? or, for these domain redirects, do i need to do what we do today where we create a second site and 301 redirect it?
Josh
Feb 13th, 2009
LYF4CE
One of the easiest ways to redirect using IIS is to just create a new web site. Most people are interested in making the http://www.yoursite.com domain the one that answers canonical URLs. Using IIS headers, simply remove the yoursite.com domain from the primary web site.
Create a new web site and make sure during the setup that you choose the path that your site’s files are located. Once the site is setup, open its properties, click the Advanced button and enter yourdomain.com as the header. Click the Home Directory tab and check the radio “A redirection to a URL” and the checkbox “A permanent redirection for this resource”.
All the files in your website will now automatically and permanently redirect to http://www.yourdomain.com.
I can see times when this could cause issues with web sites. For instance, if the developers have hard-coded yourdomain.com into the web code and the code misbehaves during the redirect. Albeit, if you’re coding like that you should have your hand slapped like a Catholic nun with a ruler, it can happen. From my experience, if this doesn’t work for your web site you have other issues that should be dealt with anyway because things aren’t being done right somewhere in the code.
Again, easy fix. Each situation will be unique and should be assessed thoroughly before implementing a solution. Sometimes the simple solutions are the best; sometimes developers need a swift kick in the pants.
Cheers!
Mar 28th, 2009
Reply to “IIS7 and 301 Permanent Redirects”