ASP.NET 4.0 SEO features: Response.RedirectPermanent()
ASP.NET 4.0 introduces some SEO improvements. Response has now new method called RedirectPermanent(). This method performs same redirect as Response.Redirect() but it uses response code 301. You can find more information about HTTP response codes from HTTP 1.1 specification, chapter 10. Status Code Definitions.
Response.Redirect() returns 302 to browser meaning that asked resource is temporarily moved to other location. Permanent redirect means that browser gets 301 as response from server. In this case browser doesn’t ask the same resource from old URL anymore – it uses URL given by Location header.
To illustrate difference between Response.Redirect() and Response.RedirectPermanent() let’s look at simple example. We need one usual ASP.NET Web Application with Global.asax file. In Global.asax file we have to implement BeginRequest() method.
C#protected void Application_BeginRequest(object sender, EventArgs e)
{
if (Request.FilePath == "/our-products.aspx")
{
Response.Redirect("/products.aspx", true);
}
if (Request.FilePath == "/about-us.aspx")
{
Response.RedirectPermanent("/about.aspx", true);
}
}
VB.NET
Protected Sub Application_BeginRequest(ByVal sender As Object, _
ByVal e As EventArgs)
If Request.FilePath = "/our-products.aspx" Then
Response.Redirect("/products.aspx", True)
End If
If Request.FilePath = "/about-us.aspx" Then
Response.RedirectPermanent("/about.aspx", True)
End If
End Sub
Now let’s run our web application and make the following requests:
- /our-products.aspx
- /about-us.aspx
In both cases we will be redirected but redirections are different. Here is my little mix of FireBug outputs for these requests and I think it is very self describing.
Response.RedirectPermanent() has also overload with one parameter, just like Response.Redirect() does. If you have pages that exist in search engines but are moved to other location in your application then Response.RedirectPermanent() helps you build redirection controlling mechanism that likes to search engine spiders.