"I've Google a fair bit about this, but I'm still confused. I have a static site built with html that I am migrating to ASP.NET 2.0. However, I don't want links to break, and need to redirect from all the old pages to the new ones. As the new ones will be generated dynamically, the urls will change a lot: it won't simply be a case of the the url changing from .htm to .aspx.I've heard that using the metatag of the .htm page is dangerous with regards to search engine crawlers. What is the best way of having this redirect happen? Is it just going through and putting a manual link on all the old .htm pages? Or is there an automatic way?"There are lots of ways you can handle redirects and newly named pages in HTML:
<customerrors mode="On" defaultredirect="GenericErrorPage.htm"> <error statuscode="403" redirect="NoAccess.htm"></error> <error statuscode="404" redirect="404.aspx"></error> </customerrors>
<configsections>
...
<sectiongroup name="legacyRedirects">
<section name="urls" type="System.Configuration.NameValueSectionHandler">
</section>
</sectiongroup>
</configsections>
<legacyredirects>
<urls>
<add key="about.html" value="About.aspx">
<add key="contact.aspx" value="contact2000.aspx">
<add key="/Website1/about.html" value="About.aspx">
<add key="news.html" value="news/bignews.asp">
<!-- For off-site links, change to Response.Redirect -->
<add key="/Website1/home.html" value="http://our.new.site.aspx">
</legacyredirects>
//Sample code only -- needs to be refactored for production
using System;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Text.RegularExpressions;
using System.Collections.Specialized;
public partial class _404 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string aspxerrorpath = null;
//TODO: refactor out and clean up all these if null if !null checks
try
{
aspxerrorpath = base.Request.QueryString["aspxerrorpath"];
}
catch(NullReferenceException ex)
{
//nothing from a .net 404
}
if (aspxerrorpath == null)
{
//is IIS giving us a 404 ?
aspxerrorpath = base.Request.ServerVariables["QUERY_STRING"];
if (aspxerrorpath != null)
{
//clean up querystring
//i.e. 404;http://localhost:80/about.htm
Regex regex = new Regex(@".*http.*:\/\/[^/]*/");
aspxerrorpath = regex.Replace(aspxerrorpath, "");
}
}
if(aspxerrorpath != null)
{
//strip out any leading slashes in file names ("/Default.aspx" --> "Default.aspx")
Regex regex = new Regex(@"^\/");
aspxerrorpath = regex.Replace(aspxerrorpath, "");
NameValueCollection config =
(NameValueCollection)ConfigurationManager.GetSection("legacyRedirects/urls");
foreach (string legacyPage in config)
{
base.Response.Write("Trying to match:" + aspxerrorpath
+ " with configuration:" + legacyPage + "
");
if (aspxerrorpath.ToLower() == legacyPage.ToLower())
{
base.Server.Transfer(config[legacyPage]);
}
}
}
}
}
Download sample code here:404_Redirect.zip (4.7kb)
Listed below are links to blogs that reference this entry: How To: Support Old Links and Redirect to New ASP.NET Pages.
TrackBack URL for this entry: http://www.rootsilver.com/mt-tb.cgi/25
Leave a comment