Application_BeginRequest method of Global.asax file can be used to rewrite the URL of the current page. Following code snippet shows you how to achieve this goal.
<%@ Application Language=”VB” %>
<script runat=”server”>
Protected Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
‘This is for dynamic redirection without having to create a directory in the root folder. We
‘needed to change the 404 in IIS under custom errors to /(forward slash) instead of the default 404b.html
If Regex.IsMatch(Request.Url.AbsoluteUri, “404;”) ‘Get the url from browser address bar
Dim UrlSplit(), url as String
UrlSplit = Request.Url.AbsoluteUri.Split(”?404;”) ‘Split the url where the error occured in this case 404
url = replace(UrlSplit(1), “404;”, “”)
url = replace(url, “http://www.youraddress.com”, “”) ‘Replaces the youraddress.com address and puts nothing
‘Select Case to check where the page should be redirect too.
Select Case url
Case “/aboutmypage”
response.Redirect(”/aboutus”) ‘ Redirects to correct url.
Case “/yourprogram”
response.Redirect(”/programs/yourprogram”)
Case “/canucks”
response.Redirect(”/sports/hockey/canucks”)
Case Else
response.redirect(”/help/sitemap/index.aspx”)
End Select
End If
End Sub
</script>