Skip to content

Instantly share code, notes, and snippets.

@brstp
Created January 10, 2012 15:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brstp/1589487 to your computer and use it in GitHub Desktop.
Save brstp/1589487 to your computer and use it in GitHub Desktop.
Custom error page for IIS to redirect removed pages. F&S/Tankbar. Installation instruction on: http://evolvedcode.net/content/code_smart404/
<%
'Redirection from old Friskis&Svettis sites to pages on new sites
'Version R2A
'Script adapted by stefan.pettersson@lumano.se 2004-01-07
'Script adapted by stefan.pettersson@lumano.se 2012-01-10
'Script patched to match substring source url (to match each F&S club file archive instead of individual files). 2012-01-23
'This ASP script originally lived at http://evolvedcode.net/
'For the latest version of this code please see;
' http://evolvedcode.net/content/code_smart404/
Function FixURL( ByVal sURI )
'Code to append current server name to be standards compliant, but first check
' that the URL is making use of an absolute path
Const csHttpPrefix = "http://"
'First of all remove the default document if they exist
sURI = StripDefaultDoc( sURI )
'Try to make the url absolute if possible
If StrComp( Left( sURI, Len( csHttpPrefix ) ), csHttpPrefix, vbTextCompare ) <> 0 And Left( sURI, 1 ) = "/" Then
'Check if we have the HOST variable to work from
FixURL = csHttpPrefix & Request.ServerVariables("HTTP_HOST") & sURI
Else
FixURL = sURI
End If
End Function
Function StripDefaultDoc( ByVal sURI )
'Code to remove the first instance of a default document name from a given URL
sURI = Replace( sURI, "/default.asp", "/", 1, 1, vbTextCompare )
sURI = Replace( sURI, "/default.html", "/", 1, 1, vbTextCompare )
sURI = Replace( sURI, "/default.htm", "/", 1, 1, vbTextCompare )
sURI = Replace( sURI, "/index.asp", "/", 1, 1, vbTextCompare )
sURI = Replace( sURI, "/index.html", "/", 1, 1, vbTextCompare )
sURI = Replace( sURI, "/index.htm", "/", 1, 1, vbTextCompare )
StripDefaultDoc = sURI
End Function
Function StripQuerystring( ByVal sURI )
'Code to strip querystring parameters to enable matching to work properly
'If InStrRev( sURI, "?" ) > 0 Then
'
' sURI = Left( sURI, InStrRev( sURI, "?" ) - 1 )
'End If
StripQuerystring = sURI
End Function
Function URLDecode( ByVal sURI )
'Code to replace encoded characters with their physical representations, this is really
' aimed at stopping over-active browsers from breaking the redirect.
Dim iChar
For iChar = 33 To 126
sURI = Replace( sURI, "%" & Hex( iChar ), Chr( iChar ), 1, 5, vbTextCompare )
Next
URLDecode = sURI
End Function
Sub ReDirect_Handle()
'Code to run the core of the smart 404 handling
Dim sSrcPage, sDstPage, sOpType
Dim iStatus
Dim sVersion, iVersion
'Check our list of redirects to see if this error is a known error and so can be
' gracefully handled, otherwise trip out a 404
sSrcPage = Redirect_Source
iStatus = 0
'Determine the the version number of the server software, this will then
' be used later on to enable us to check if it supports server.transfer
' i.e. IIS 5 or later.
sVersion = Request.ServerVariables("SERVER_SOFTWARE")
sVersion = Right( sVersion, Len( sVersion ) - InStrRev( sVersion, "/" ) )
If IsNumeric( sVersion ) Then
iVersion = CInt( sVersion )
End If
If sSrcPage <> vbNullString Then
iStatus = ReDirect_Known( sSrcPage, sDstPage, sOpType )
End If
'Check if we have set a return status code
If iStatus > 0 Then
'Test if the output from the redirect code is numeric
If ISNumeric( Left( sOpType, 3 ) ) Then
'Compare the status code and the status text
If CInt( Left( sOpType, 3 ) ) = CInt( iStatus ) Then
'Assign the returned status text
Response.Status = sOpType
End If
End If
If iStatus = 301 Or iStatus = 302 Then
Response.AddHeader "Location", sDstPage
%><html>
<head>
<title>Object moved</title>
<meta name="robots" content="noindex" />
</head>
<body>
<h1>Object moved</h1>
The requested object can currently be found <a href="<%= sDstPage %>">here</a>.
</body>
</html>
<%
ElseIf iStatus = 403 Then
%><html>
<head>
<title>Access Denied</title>
<meta name="robots" content="noindex nofollow" />
</head>
<body>
<h1>Access Denied</h1>
Access to the requested resource has been denied.
</body>
</html>
<%
ElseIf iStatus = 1001 And iVersion >= 5 Then
'Server.Transfer will not work with pages which include a
' querystring so strip it if there is one
sDstPage = StripQuerystring( sDstPage )
Server.Transfer( sDstPage )
End If
'Set headers and any content so stop executing the script
Response.End
Else
'unable to locate a suitable handler
Response.Status = "404 Not Found"
End If
End Sub
Function ReDirect_Source()
'Figure out which page triggered this page
Dim sPage
ReDirect_Source = vbNullString
sPage = Request.QueryString
'Ensure this is an error message we are capable of handling
If Left( sPage, 3 ) = "404" Then
'Check there is a semi-colon
If InStr( 1, sPage, ";" ) > 0 Then
'Drop everything before the semi-colon
sPage = Right( sPage, Len( sPage ) - InStr( 1, sPage, ";" ) )
'Tidy whats left of the url
sPage = StripQuerystring( sPage )
sPage = StripDefaultDoc( sPage )
sPage = URLDecode( sPage )
'Attempt to strip out the server name
If InStr( Len( "http://" )+1, sPage, "/" ) > 0 Then
sPage = Right( sPage, Len( sPage ) - (InStr( Len( "http://" )+1, sPage, "/" )-1) )
End If
'Attempt to drop trailing forward slash if present
If Right( sPage, 1 ) = "/" And sPage <> "/" Then
sPage = Left( sPage, Len( sPage ) - 1 )
End If
'Assuming anything is left then return the data
If Len( sPage ) > 0 Then
ReDirect_Source = LCase( sPage )
End If
End If
End If
End Function
Sub ReDirect_Add( ByVal sSource, ByVal sDestination, ByVal sType, ByRef sRedirects() )
'Code to add extra re-directs into the redirection array
'Drop any trailing slash on a sub-directory
If Right( sSource, 1 ) = "/" And sSource <> "/" Then
sSource = Left( sSource, Len( sSource ) - 1 )
End If
'If the initial index has been populated then add another index
If sRedirects(0,0) <> vbNullString Then
ReDim Preserve sRedirects(2, UBound( sRedirects, 2 )+1 )
End If
'Populate this new index
sRedirects(0,UBound( sRedirects, 2 )) = sSource
sRedirects(1,UBound( sRedirects, 2 )) = sDestination
sRedirects(2,UBound( sRedirects, 2 )) = sType
End Sub
Function ReDirect_Known( ByVal sSrcURL, ByRef sDstURL, ByRef sOpType )
'Code to test if a given url has been assigned a redirection or not,
' and if it has to retrieve the destination and the operation type
Dim xmlDoc, xmlNode
Dim sXPath
Dim sReDir(), iRedirect
ReDim sReDir(2,0)
'Fail closed
ReDirect_Known = 0
'Populate the redirections as required
'REPLACE_THIS_ROW_WITH_REDIRECT_DATA
' ReDirect_Add "__old_url__" , "__new_url__", "PERM", sReDir
ReDirect_Add "/webarchive/data_foreningar/haninge/" , "http://haninge.friskissvettis.se/index.php", "PERM", sReDir
ReDirect_Add "/webarchive/data_foreningar/huddinge/" , "http://huddinge.friskissvettis.se/index.php", "PERM", sReDir
ReDirect_Add "/webarchive/data_foreningar/lidingo/" , "http://lidingo.friskissvettis.se/index.php", "PERM", sReDir
ReDirect_Add "/webarchive/data_foreningar/norrtalje/" , "http://norrtalje.friskissvettis.se/index.php", "PERM", sReDir
ReDirect_Add "/webarchive/data_foreningar/sodertalje/" , "http://sodertalje.friskissvettis.se/index.php", "PERM", sReDir
ReDirect_Add "/webarchive/data_foreningar/taby/" , "http://taby.friskissvettis.se/index.php", "PERM", sReDir
ReDirect_Add "/webarchive/data_foreningar/akersberga/" , "http://akersberga.friskissvettis.se/index.php", "PERM", sReDir
'Cycle through all the redirects
For iRedirect = LBound( sReDir, 2 ) To UBound( sReDir, 2 )
'Check if the source url matches the source redirect url
' If StrComp( sReDir( 0, iRedirect ), sSrcURL, vbTextCompare ) = 0 Then
' If string2 is found within string1 - InStr returns the position at which match is found
'
' Patched here for F&S to match sub string (in file archive)
If InStr(1, sSrcURL, sReDir( 0, iRedirect ), vbTextCompare) > 0 Then
sDstURL = sReDir( 1, iRedirect )
Select Case LCase( sReDir( 2, iRedirect ) )
Case "perm"
'Permanent redirect
sOpType = "301 Moved Permanently"
sDstURL = FixURL( sDstURL )
ReDirect_Known = 301
Case "temp"
'Temporary redirect
sOpType = "302 Moved Temporarily"
sDstURL = FixURL( sDstURL )
ReDirect_Known = 302
Case "deny"
'Access forbidden
sOpType = "403 Access Denied"
ReDirect_Known = 403
Case "virt"
'Temporarily alias the requested file
sOpType = "Virtual Alias"
ReDirect_Known = 1001
Case Else
'Request type was unknown
Response.Write "Unknown method encountered"
End Select
End If
Next
End Function
'Run the error handling code
ReDirect_Handle
'Assign this request a 404 status code and display the rest of their text
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Sidan kan inte hittas</title>
<meta name="robots" content="noindex follow" />
<style type="text/css">
p,li {
font-weight: normal;
color: black;
font-size: 10px;
font-family: Verdana, Arial, Helvetica, Sans-serif;
line-height: 14px;
text-decoration: none;
padding-left: 26px;
}
li {
margin-left: 20px;
padding-left: 16px;
line-height: 200%;
}
h1 {
padding: 26px;
font-weight: bold;
color: black;
font-size: 13px;
line-height: 17px;
font-family: Verdana, Arial, Helvetica, Sans-serif;
text-decoration: none;
}
table {
width: 800px;
border: none;
border-spacing: 2px;
}
th {
height: 114px;
}
.box {
padding-left: 30px;
}
.bottom {
background: #808185;
height: 20px;
}
.menu {
background: #808185;
height: 15px;
}
</style>
<link rel="stylesheet" href="http://www.google.com/cse/style/look/default.css" type="text/css" >
</head>
<body bgcolor="white">
<a name="top"></a>
<center>
<table>
<tr class="top"><th><img src="http://web.friskissvettis.se/WebArchive/navi/20060817093058.gif" alt=""></th></tr>
<tr class="menu"><td></td></tr>
<tr>
<td>
<h1>Vi kan tyv&auml;rr inte hitta sidan som du var p&aring; v&auml;g till</h1>
<p>Vi h&aring;ller p&aring; att flytta flera webbsidor. Kanske &auml;r det d&auml;rf&ouml;r som vi inte hittar sidan du var p&aring; v&auml;g till.</p>
<p>Du kan prova n&aring;got av dessa s&auml;tt f&ouml;r att hitta vidare:</p>
<ol>
<li>G&aring; till <a href="http://www.friskissvettis.se">Friskis&amp;Svettis hemsida</a>. D&auml;r finns l&auml;nkar till alla st&auml;llen d&auml;r du kan tr&auml;na.</li>
<li><a href="JAVASCRIPT:window.history.back();">G&aring; tillbaka till sidan du kom i fr&aring;n</a>.</li>
<li>S&ouml;k p&aring; friskissvettis.se i f&auml;ltet h&auml;r nedanf&ouml;r.</li>
</ol>
<div class = "box">
<div id="cse" style="width: 100%;">Loading</div>
<script src="http://www.google.com/jsapi" type="text/javascript"></script>
<script type="text/javascript">
google.load('search', '1', {language : 'sv'});
google.setOnLoadCallback(function() {
var customSearchControl = new google.search.CustomSearchControl(
'016527757011980477505:ajssnxpmay0');
customSearchControl.setResultSetSize(google.search.Search.FILTERED_CSE_RESULTSET);
customSearchControl.draw('cse');
}, true);
</script>
</div> <!-- /box -->
<h1>We are sorry, we can not find the page you were looking for</h1>
<p>We are in the processes of moving some pages around.</p>
<p>Please try any of these options:</p>
<ol>
<li>Go to <a href="http://www.friskissvettis.se">the home page of Friskis&amp;Svettis in English</a>.</li>
<li><a href="JAVASCRIPT:window.history.back();">Go back to the previous page</a>.</li>
<li>Search for the page in the form field above.</li>
</ol>
</td>
</tr>
<tr class="bottom"><td>&nbsp;</td></tr>
</table>
</center>
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-3818105-1");
pageTracker._trackPageview();
</script>
<!-- 404.asp 2012-01-20T16:00:00+1-->
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment