Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 2nd, 2012  |  syntax: None  |  size: 5.90 KB  |  hits: 19  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Using web.config - Redirecting browsers with 301, 302, and 307 status codes on IIS 7 and IIS 7.5
  2. 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.
  3.  
  4. 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.
  5.  
  6. What are http response redirects?
  7. 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.
  8.  
  9. Web.config based redirects
  10.  
  11.     <httpRedirect enabled="true" destination="http://foo.com" httpResponseStatus="Permanent" /> <!-- 301 permanent redirect -->
  12.     <httpRedirect enabled="true" destination="http://foo.com" httpResponseStatus="Found" /> <!-- 302 found redirect -->
  13.     <httpRedirect enabled="true" destination="http://foo.com" httpResponseStatus="Temporary" /> <!-- 307 temporary redirect -->
  14.     <httpRedirect enabled="true" destination="http://www.foo.com/foo.htm" exactDestination="true" /> <!-- 302 (found) redirect, to a specific page or directory -->
  15.  
  16. Redirect status codes
  17. 301 permanent redirect - Moved permanently
  18. The requested resource has been assigned a new permanent URI and any future references to this resource should use the new URL.
  19.  
  20.     Permanently redirect a site or subdirectory to another domain.
  21.     <httpRedirect enabled="true" destination="http://foonew.com" httpResponseStatus="Permanent" />
  22.  
  23.     Permanently redirect a site or subdirectory to a subdirectory on the same domain.
  24.     <httpRedirect enabled="true" destination="http://foo.com/newdir" httpResponseStatus="Permanent" />
  25.  
  26.     Permanently redirect a site or subdirectory to a specific page.
  27.     <httpRedirect enabled="true" destination="http://foo.com/foo.htm" exactDestination="true" httpResponseStatus="Permanent" />
  28.  
  29. 302 found redirect
  30. 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
  31.  
  32.     Redirect a site or subdirectory to a specific page.
  33.     <httpRedirect enabled="true" destination="http://foo.com/overloaded.txt" exactDestination="true" httpResponseStatus="Found" />
  34.  
  35. 307 temporary redirect - Temporary redirect
  36. 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
  37.  
  38.     Temporarily redirect a site or subdirectory to a specific page.
  39.     <httpRedirect enabled="true" destination="http://foo.com/overloaded.txt" exactDestination="true" httpResponseStatus="Temporary" />
  40.  
  41.  
  42. Using HTTP redirects
  43.  
  44.     Use a text editor to create a file named web.config
  45.     Save the web.config file with the appropriate content
  46.     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
  47.  
  48.  
  49. Detailed web.config content
  50. Let's redirect foo.com/olddir to somewhere else.
  51.  
  52.     If there isn't an existing web.config in the "olddir" directory, your new web.config should look something like this
  53.  
  54.     <?xml version="1.0"?>
  55.       <configuration>
  56.         <system.webServer>
  57.           <httpRedirect enabled="true" destination="http://foo.com/newdir" httpResponseStatus="Permanent" />
  58.         </system.webServer>
  59.       </configuration>
  60.  
  61.  
  62.     If there is an existing web config, without a <system.webServer> section... Your new web.config should look like this
  63.  
  64.     <?xml version="1.0"?>
  65.       <configuration>
  66.         <system.web>
  67.           ..existing text..
  68.           ..existing text..
  69.         </system.web>
  70.         <system.webServer>
  71.           <httpRedirect enabled="true" destination="http://foo.com/newdir" httpResponseStatus="Permanent" />
  72.         </system.webServer>
  73.       </configuration>
  74.  
  75.  
  76.     If your existing web.config already has a <system.webServer> section, just add the <httpRedirect> section
  77.  
  78.     <configuration>
  79.       <system.web>
  80.         .. existing text ..
  81.         .. existing text ..
  82.       </system.web>
  83.       <system.webServer>
  84.         <security>
  85.           <ipSecurity allowUnlisted="true">
  86.             <add ipAddress="83.116.19.53"/>
  87.             <add ipAddress="83.116.119.0" subnetMask="255.255.255.0"/>
  88.           </ipSecurity>
  89.         </security>
  90.         <httpRedirect enabled="true" destination="http://foo.com/newdir" httpResponseStatus="Permanent" />
  91.         <modules runAllManagedModulesForAllRequests="true"/>
  92.       </system.webServer>
  93.     </configuration>
  94.  
  95.  
  96. Redirecting specific pages
  97. 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.
  98.  
  99. <?xml version="1.0"?>
  100.   <configuration>
  101.     <location path="bing.htm">
  102.       <system.webServer>
  103.         <httpRedirect enabled="true" destination="http://bing.com" httpResponseStatus="Permanent" />
  104.       </system.webServer>
  105.     </location>
  106.     <location path="google.htm">
  107.       <system.webServer>
  108.         <httpRedirect enabled="true" destination="http://google.com" httpResponseStatus="Permanent" />
  109.       </system.webServer>
  110.     </location>
  111.     <location path="yahoo.htm">
  112.       <system.webServer>
  113.         <httpRedirect enabled="true" destination="http://yahoo.com" httpResponseStatus="Permanent" />
  114.       </system.webServer>
  115.     </location>
  116.   </configuration>