No just a bit of tidying up.
Ok this is the
original thread and the code had a couple of rewrites to fix various
bugs undocumented features. Final version
was posted here
This is a revised and improved version. IMO of course
this is for 404.asp
Code:
<%
' change the constant value to point to your list of URIs
const c_sURIList = "/urilist.txt"
dim l_iLoop, m_sRedirectTo, m_sRequest, m_asPages()
dim m_bFound : m_bFound = false
redim preserve m_asPages(1, 0)
if CheckFile(c_sURIList) then
LoadArray(c_sURIList)
end if
for l_iLoop = 0 to ubound(m_asPages,2)
if m_asPages(0,l_iLoop) <> "" then
if instr(request.servervariables("QUERY_STRING"), m_asPages(0,l_iLoop)) <> 0 then
m_sRequest = replace(request.servervariables("QUERY_STRING"),"404;","")
m_sRedirectTo = replace(m_sRequest,m_asPages(0,l_iLoop), m_asPages(1,l_iLoop))
m_bFound = true
end if
end if
if m_bFound then exit for
next
if m_bFound then
response.status = "301 Moved Permanently"
response.addheader "Location", m_sRedirectTo
response.end()
else
%><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Not Found</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
This is the 404 page
<br />
<br />
Add in whatever text or navigation you want to show the user for pages that are NOT redirected
</body>
</html>
<%
response.status = "404 Not Found"
response.end
end if
private sub ArrayInsert(p_sOldURI, p_sNewURI)
redim preserve m_asPages(1, ubound(m_asPages,2)+1)
m_asPages(0,ubound(m_asPages,2)) = p_sOldURI
m_asPages(1,ubound(m_asPages,2)) = p_sNewURI
end sub
public function CheckFile(p_sFileName)
' Check that a file exists first
dim l_oFSO
set l_oFSO = createobject("Scripting.FileSystemObject")
CheckFile = l_oFSO.FileExists(server.mappath(p_sFileName))
set l_oFSO = nothing
end function
public sub LoadArray(p_sFileName)
' read in the URI LIst
dim l_oFSO, l_oFSFile
dim l_sLine
set l_oFSO = createobject("Scripting.FileSystemObject")
set l_oFSFile = l_oFSO.OpenTextFile(server.mappath(p_sFileName))
do until l_oFSFile.AtEndOfStream
l_sLine = split(l_oFSFile.ReadLine,",")
ArrayInsert trim(l_sLine(0)), trim(l_sLine(1))
loop
l_oFSFile.close
set l_oFSFile = nothing
set l_oFSO = nothing
end sub
%>
the URI list is a comma separated list of old URIs and new URIs. Each pair should be on a new line.
Code:
/dir/pagename.htm,/dir/newpagename.asp
/dir/pagename1.htm,/dir/newpagename1.asp
/dir/pagename2.htm,/dir/newpagename2.asp
/dir/pagename3.htm,/dir/newpagename3.asp
/dir/pagename4.htm,/dir/newpagename4.asp
Upload the 404.asp to your website root and set your 404 error document to URL and type in /404.asp as the document location, if the script doesn't find the URI list file the array will be empty and you should get a 404 response and the page text for a non-existent file. Add a non-existent URI to the list and a page to redirect, this time you should get a 301 response and the new page should be served out.
zip file attached with the two files included.
I will do a write up of the code on
www.modtalk.co.uk as well.