Showing posts with label Seo. Show all posts
Showing posts with label Seo. Show all posts

February 23, 2011

There are few different ways to redirect a web page to another page in asp.net. Redirection can be achieved by using JavaScript, meta tags and by using asp.net methods as well. In this blog post, I will show how to setup redirection using each method and what are the pros and cons of it.

JavaScript:

Adding redirect using JavaScript involves only one line of code. The following is the code added in a content page.

Redirect using JavaScript
  1. <%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Redirection.aspx.cs" Inherits="WebApplication1.Redirection" %>
  2. <asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
  3.    <script type="text/javascript">
  4.     window.location = "default.aspx";
  5. </script>
  6.  
  7. </asp:Content>
  8. <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
  9. </asp:Content>

When the page is run, content from the master page is loaded  first and then the page redirects to new page using HTTP status code of 200. The HttpFox report from FireFox is below.

javascript-httpfox

As can be seen from Http watch report, the page is loaded first and then the page is redirected. The problem with this approach is that redirect would not work if JavaScript is disabled by the user and also search engines are fooled to think that the page is ok when it is not.

META Tags:

Secondly, meta tags can be used to redirect pages. An example code is below.

Redirect using Meta tags
  1. <%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Redirection.aspx.cs" Inherits="WebApplication1.Redirection" %>
  2. <asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
  3. <meta http-equiv="refresh" content="0;url=default.aspx" />
  4. </asp:Content>
  5. <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
  6. </asp:Content>

The Http watch report is below.

meta-httpwatch

As can be seen, the css file is loaded first and then the page redirects using Http status code of 200. Similar to the JavaScript approach,  the problems are that someone can stops meta refreshes from browser settings and search engines are fooled to think that the page is valid.

ASP.Net Redirect

In asp.net, pages can be redirected using 2 methods. The first method is by using the method – Response.Redirect(string url). Example code is below.

Response.Redirect(string url)
  1. namespace WebApplication1
  2. {
  3.     public partial class Redirection : System.Web.UI.Page
  4.     {
  5.         protected void Page_Load(object sender, EventArgs e)
  6.         {
  7.             Response.Redirect("default.aspx");
  8.         }
  9.     }
  10. }

Http watch report is below.

asp-net-302-httpwatch

As can be seen from the code, the page is redirected straight way using http status code of 302. This is a server side redirect and cannot be controlled by the browser. Well, a user may stop loading the page by clicking the Stop button in the browser but I am not counting that.

The main drawback of this approach is that it uses a 302 redirect. This means it is a temporary redirect which is bad for SEO.

Previously, asp.net redirect could be used differently to allow 301 redirect. 301 http status codes means the page has moved permanently and is search engine friendly. Here is a sample code.

asp.net 301 redirect
  1. namespace WebApplication1
  2. {
  3.     public partial class Redirection : System.Web.UI.Page
  4.     {
  5.         protected void Page_Load(object sender, EventArgs e)
  6.         {
  7.             Response.Status = "301 Moved Permanently";
  8.             Response.AddHeader("Location", "default.aspx");
  9.         }
  10.     }
  11. }

Http watch report is below.

asp-net-301-httpwatch

In the new release of .NET 4.0, Microsoft has added a new method Response.RedirectPermanent(string url). This method also allows for 301 redirects. Sample code is below.

RedirectPermanent(string url)
  1. namespace WebApplication1
  2. {
  3.     public partial class Redirection : System.Web.UI.Page
  4.     {
  5.         protected void Page_Load(object sender, EventArgs e)
  6.         {
  7.             Response.RedirectPermanent("default.aspx");
  8.         }
  9.     }
  10. }

The http watch report produced when the page is run is below.

asp-net-301-permanent-httpwatch

Reference: Shahed Kazi at AspNetify.com