Thursday, May 28, 2009

301 Redirect using Asp.net and asp

Many times we move our webpages or our entire website from one domain to another or change the address of some of the pages inside the website.

All such moving of pages is painful for a user who simply deletes the pages from the old server and copies them to the new server or maps them to the new server or domain.

In such a scenario a user looses every SEO which was related to his links earlier.So for preventing such type of SEO loss we make use of 301 Redirects.

301 Redirects stand for moved permanently.This tells the browser that this page is moved permanently from the old location to the new location.The benefit of using 301 Redirects is that the search engine,crawlers and other bots respect 301 redirects and preserve and SEO information related to the old link, It simply tells the search engines that the page location has permanently moved and you need to index the page from the new location in future.

There is also another form of redirect known as 302 redirect but this redirect is the temporary redirect thus its also not beneficial from the SEO point of view.

301 redirects can be achieved in many ways and ways are different for different type of programming languages and operating systems of the server which you are using.

301 redirects can be very easily achieved by using .htaccess file if you are using linux servers.

But if you programmatically want to impose a 301 moved permanently redirect then you can easily do this by placing this piece of code on your aspx page if you are using asp.net

<script runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location","http://www.smallworkarounds.com/jqnetgrid/jqnetgridhome.aspx");
}
</script> 

Placing this code in your aspx page will redirect your page whenever it loads to a page specified in the Response.AddHeader parameter

Response.Status is used to make entry in the header which says that this page has been permanently moved to another location.

Also same 301 redirection can be achieved if we place the same piece of code inside the Page_Load method in the code behind file

protected void Page_Load(object sender, System.EventArgs e)
        {
		       string NewMedUrl = "http://www.smallworkarounds.com/jqnetgrid/jqnetgridhome.aspx";
                Response.Clear();
                Response.Status = "301 Moved Permanently";
                Response.AddHeader("Location", NewMedUrl);
                Response.End();
        }

In classic asp to achieve the same functionality we would do

<%@ Language=VBScript %>
<%
Response.Status="301 Moved Permanently"
Response.AddHeader "Location", "http://www.smallworkarounds.com/jqnetgrid/jqnetgridhome.asp"
%>

Also please make are sure recheck whether your pages are redirecting correctly or not after writing the redirect code.

There are few other ways to redirect your pages using 301 moved permanently redirect.But this is the most efficient way to redirect pages using the code.

At the IIS level you can also set the redirect to property of any file to make it redirected to the desired location.

1 comments :

yogesh said...

Good one really was very helpful to freashers thanks Aashish

Also i use 301 redirect checker to confirm the same.

Post a Comment