sim.plified.com

Chris Pollock

Chris Pollock - web developer (PHP/mySQL & ASP.NET)
undivided… my thoughts on world, family, church, business, technology and Jesus Christ (all in all)

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>

 

Technorati Tags: ,,

2 Comments, Comment or Ping

  1. 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

  2. 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!

Reply to “IIS7 and 301 Permanent Redirects”

Things I see

Amy and baby after storm cloud bike rideRochester storm cloudHuge storm cloud Rochester NYSad life preserver SimonHappy life preserver SimonFuture employee?Simon rides jasmineMicah with ear protectionMicah with ear protectionCIMG7660100_3672100_3666100_3661100_3650100_3643

Chris Pollock

Web Developer - proficient in both PHP and ASP.NET.
Rochester, New York

View my web developement site.

View Chris Pollock's LinkedIn profile

My Pictures

Amy and baby after storm cloud bike rideRochester storm cloudHuge storm cloud Rochester NYSad life preserver SimonHappy life preserver SimonFuture employee?Simon rides jasmineMicah with ear protectionMicah with ear protectionCIMG7660100_3672100_3666100_3661100_3650100_3643