I just set up my site to redirect old htm and aspx pages to the new DNN URLs and to send a Response 301 "Moved Permanently" for the benefit of search engine robots. I did not notice the SiteUrls.config file until I was done, but it probably would not have sent the 301 code anyway. Here is how I did it:
1. Set the custom error page for 404 errors in IIS to my custom ErrorRedirect.aspx page. My site is hosted on webhost4life, and they provided a control panel form to set this value. This step is necessary to handle non-ASP.NET pages, such as .htm and .asp, which are not affected by the web.config setting below.
2. To redirect old .aspx pages, I added an error element to web.config to redirect 404 errors to my custom ErrorRedirect.aspx page:
<customErrors mode="RemoteOnly">
<error statusCode="404" redirect="/ErrorRedirect.aspx" />
</customErrors>
3. I wrote the ErrorRedirect.aspx page shown below and deployed it to the root folder. If you use this page, replace yourwebsitename.com with your actual site name, correct the tab numbers and e-mail address as needed, and modify the SELECT CASE statement to include the redirects you desire.
<PRE><%@ Language="vb" %>
<HTML>
<script runat="server" language="vb">
' This sub redirects from old files to new files at new locations
' or with a new URL.
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
Dim strRedirectUrl As String
Dim strRawUrl As String = Request.RawUrl
' Example RawUrl:
' /errorredirect.aspx?404;http://www.yourwebsitename.com:80/index.htm
' /ErrorRedirect.aspx?aspxerrorpath=/Useful_Sites.aspx
Trace.Write("ErrorRedirect.Page_Load", String.Format("RawUrl = {0}", strRawUrl))
Dim strSourceUrl As String = Request.QueryString("aspxerrorpath")
If strSourceUrl Is Nothing OrElse strSourceUrl.Length = 0 Then
If strRawUrl.IndexOf("?404") > 0 Then
strSourceUrl = strRawUrl.Substring(strRawUrl.LastIndexOf("/"))
' Else leave strSourceUrl empty.
End If
End If
Trace.Write("ErrorRedirect.Page_Load", String.Format("strSourceUrl = {0}", strSourceUrl))
Select Case strSourceUrl.ToLower()
Case "/useful_sites.aspx"
strRedirectUrl = "http://www.yourwebsitename.com/UsefulSites/tabid/52/Default.aspx"
Case "/index.htm", "/index.html", "/default.htm", "/default.html", "/default.asp"
strRedirectUrl = "http://www.yourwebsitename.com/"
Case Else
strRedirectUrl = String.Empty
End Select
If strRedirectUrl.Length > 0 Then
Response.Status = "301 Moved Permanently"
Response.AddHeader("Location", strRedirectUrl)
Response.End
End if
Catch ex As Exception
Trace.Warn("ErrorRedirect.Page_Load", ex.ToString())
' Show this error page.
'Response.Redirect("http://www.yourwebsitename.com/")
End Try
End Sub
Private Sub Send_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
System.Web.Mail.SmtpMail.Send(txtEmail.Text, "support@yourwebsitename.com", "yourwebsitename.com: 404 - Page Not Found", txtDescription.Text)
lblFeedback.Visible = False
lblResult.Text = "E-mail has been sent. Thank you."
End Sub
</script>
<head>
<title>404 - Page Not Found</title>
<meta name="robots" content="noindex" />
</head>
<body>
<h1>404 - Page Not Found</h1>
Sorry but the page you were looking for has not been found. We tried our best to direct you
to a suitable place automatically but we did not find a suitable match...
<h3>What can you do now?</h3>
Well you could try <a href="JAVASCRIPT:window.history.back();">going back</a> to the last page
you visited and then browsing somewhere else, alternatively you could visit our <a href="/">front page</a>
and try browsing your way down from there - whatever you're looking for should be available either
directly from the front page or from one of the sections which are linked from the front page.
<h3>If all else fails...</h3>
If you think that the link you just followed really should work and you've tried quite hard to
access it then drop us an e-mail explaining which link keeps bringing you to this page and where you
found it. Most of the time these sorts of problems are automatically registered somewhere and somebody
will be dealing with it, but on the off-chance that for some reason this process has failed if you
drop us an e-mail somebody will look into it.
<form runat="server">
<p>
<asp:Label id="lblFeedback" runat="server">
Please describe what the error was to the administrators so this can get fixed:
<table>
<tr><td>Email:</td><td><asp:TextBox id="txtEmail" runat="server"/></td></tr>
<tr><td colspan=2>Description:<br /><asp:TextBox id="txtDescription" TextMode="multiline" height=200 width=400 runat="server"/></td></tr>
</table>
<asp:Button id="btnSend" onclick="Send_Click" Text="Send" runat="server"/>
</asp:Label>
<asp:Label id="lblResult" runat="server" />
</p>
</form>
</body>
</HTML>
</PRE>