Web.config httpRedirect - Redirecting individual pages with 301, 302, and 307 status codes

clock February 7, 2012 23:23 by author 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 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>
 
 

 



Web.config httpRedirect - Redirecting entire sites and directories with 301, 302, and 307 status codes - Part two

clock February 7, 2012 20:28 by author 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 wildcard redirect an entire site or directory. The redirect acts as a wildcard redirect. If the browser visits /old/file55.txt, they would be redirected to /new/file55.txt. You are currently on part two of this article. Part one explains what http redirect status codes are, and provides several example web.config files. 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.

Example one - same site, new directory
So your site is up and running for a while before you notice that there is a typo in one of your directories. You have site.tld/baksets, but it should be site.tld/baskets. The "baksets" directory is well indexed by google, and you know that many people have bookmarked pages in the "baksets" directory.

What to do? You want to retain your current search engine rankings, and you want your customer bookmarks to work too.

  • First you would prepare a web.config file similar to the one shown below.
  • Next you would rename your existing "baksets" directory to baskets.
  • Then you would recreate the "baksets" directory and place the web.config file in it.
  • Finally, you would test a few links to make sure everything works.

I have placed the web.config show below in the /examples/webconfig/redir/baksets/ directory. This will redirect all requests, for any file, to the same file in the target directory or remote site. Let's see it in action.

<?xml version="1.0"?>
 <configuration>
   <system.webServer>
      <!--redirect requests from dir1 to dir2-->
      <!-- 301 permanent redirect -->
      <httpRedirect enabled="true" destination="http://www.victor-ratajczyk.com/examples/webconfig/redir/baskets" httpResponseStatus="Permanent" />
   </system.webServer>
 </configuration>
 
 

 


Example two - entire directory, redirects to a single file
So now you have completely redesigned your site. The new layout is completely different. On top of directories having new names, all the file names are different too.

What to do? Without a lot of work, there is no way to do matching page-for-page redirects. But at least we can make sure that bookmark users and search engines are redirected to the proper section of our redesigned site.

I have placed the web.config show below in the /examples/webconfig/redir/basketgifts/ directory. This will redirect all requests, for any file, to a single file in the target directory or remote site. Let's see it in action.

 

<?xml version="1.0"?>
 <configuration>
   <system.webServer>      
      <!-- 301 permanent redirect -->
      <httpRedirect enabled="true" destination="http://www.victor-ratajczyk.com/examples/webconfig/redir/giftbaskets/index.htm" exactDestination="true" httpResponseStatus="Permanent" />
   </system.webServer>
 </configuration>
 
 







Web.config httpRedirect - Redirecting entire sites and directories with 301, 302, and 307 status codes - Part one

clock February 7, 2012 18:23 by author 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>