301 Redirect with ASP.NET
If you move a page on a website to a different address it can have an impact both on your search engine ranking and on your site’s overall usability and accessibility. I talked a while ago about the importance of using custom error pages on a site and discussed specifically the custom “404 page not found” and making that more friendly and informative for visitors who mistyped a page or simply looked for a page that wasn’t there.
404 error pages are designed to deal with problems based on a page simply being unavailable or misspelt. But what about if you had a page at that address but have since moved it? Perhaps you changed the page’s URL for better search engine optimisation, or restructured your site so that it is now in a subfolder on the site. A 404 error page in that situation is likely to make a visitor think that the page is simply no longer there, and so the information on that page and the site in general will seem less accessible. Additionally, without further information a search engine will also think that page no longer exists, and any ranking and importance that the information had gained over time on that search engine won’t be transferred to the page at the new address.
This is easily solved by a 301 redirect. This automatically redirects the browser without delay to a specified page. It works both in terms of site usability, as it eliminates the need for visitors to search for the information again, but it also tells search engines such as Google that the page has moved permanently and so the search engine will update its records and you won’t lose out on any page weight or ranking that would otherwise have been lost.
As an example, say we have a page called oldpage.aspx which is in the root directory of Fog of Eternity. But I’ve restructured the site and the information on that page has now moved to newpage.aspx. Setting a 301 redirect is easily done with .NET.
I work with Visual Studio and keep my c# code seperate from my .aspx code, so I’m going to have two files:
oldpage.aspx and
oldpage.aspx.cs
Remove all the code from oldpage.aspx barring the very first line, so the entire content of the page should look something like this:
<%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”oldpage.aspx.cs” Inherits=”oldpage” %>
Then I’m going to add the following code to oldpage.aspx.cs
private void Page_Load(object sender, System.EventArgs e)
{
Response.Status = “301 Moved Permanently”;
Response.AddHeader(”Location”,”http://www.fogofeternity.com/newpage.aspx”);
}
The bolded section is the code that I’m adding, the rest of the code is standard. This should work equally well if you are using VB.NET rather than c#.
If you’re not working with a seperate code file, and instead are incorporating your scripts in the main .aspx page, simply remove the original opening line, and instead place:
<script runat=”server”>
before the line starting private void… and then place:
</script>
as the last line of the .aspx page.
Whenever a browser tries to access oldpage.aspx now, its very first action will be to redirect to newpage.aspx, and the user shouldn’t even notice a difference in their browsing. It’s obviously worth checking that this works yourself once it’s been implemented. And even though the redirect is there, it’s still worthwhile to update any links on your site that need to point to the new page, and as much as possible to inform anyone linking to that page from elsewhere of the new address.
It’s a pretty simple bit of code that takes only a few minutes to implement, but has a significant effect on how usable and generally accessible your site is, and also has benefits for search engine optimisation as well.

June 13th, 2008 at 11:58 am
This is particularly important for SEO. If you’re moving content and want your Google-juice to be carried over to the new location then you have to make it a 301 re-direct. Using Response.Redirect() on its own will send the wrong status code (i.e. “302 Moved”). The user will still get to the new page, but the search engine won;t count it.
Bombay Crows last blog post..Spider web pages in ASP.NET - reading and parsing HTML files
June 13th, 2008 at 12:54 pm
Yeah, I think just having Response.Redirect() is a common mistake, because in terms of functionality it gets the job done. Those are in some cases the most difficult errors to track down, because there isn’t a code error as such, it’s something that you have to know about in advance or it becomes a potentially serious oversight.