Creating 301 redirects in web.config
For various reasons at times you may need to create a 301 redirect to another URL. This could be as a result of a page moving or you just need to create some friendly URLS.
As a developer you may be tempted to do something like this in code...
1private void Page_Load(object sender, System.EventArgs e)2{3 Response.Status = "301 Moved Permanently";4 Response.AddHeader("Location","http://www.new-url.com");5}
But do you really want your project cluttered up with files who's only purpose is to redirect to another page!
You may also be tempted to try doing something with .NET's RouteCollection. This would certainly solve an issue on creating a redirect for anything without a file extension, but there is a better way.
In your web.config file under the configuration node create something like this
1<location path="twitter">2 <system.webServer>3 <httpRedirect enabled="true" destination="http://twitter.com/TwitterName" httpResponseStatus="Permanent" />4 </system.webServer>5</location>
The location path specifies that path on your site that this redirect will apply to. The destination value in the httpRedirect is where the redirect will go to. As well as setting Permanent for the httpResponseStatus you can also specify Found or Temporary depending on your needs.