My office is undergoing a huge redesign, and all pages are being moved and/or having their extensions changed (from .htm to .cfm). Our webmaster has dictated that we need to do a redirect for every single page so that no one gets a 404 error.
We’re talking thousands upon thousands of pages, and after getting some good advice in the Server Management forum here on Sitepoint, I came up with a way to do the redirects sort of automatically.
Since we’ve been keeping an Excel spreadsheet of the old and new page locations, I decided to import that spreadsheet into our database, and use the database to handle the redirects.
Basically, I built a custom 404 page and had our server guy point the webserver to it. The page takes the old url and queries the database to find it. If it finds a match, it plugs the new url for that page into a meta refresh tag and displays a message that the page has been moved. If no match is found, it displays the standard 404 message.
Since I want search engines to know that the pages have been moved, I decided to stuff a cfheader tag into the mix, but I’m not certain I’m doing it right. This is what I have in the head section of the 404 page:
<cfquery name="redirect" datasource="#ourdsn#">
SELECT new_url
FROM redirects
WHERE old_url = <cfqueryparam value="#requeststring#" cfsqltype="cf_sql_varchar">
</cfquery>
<cfif #redirect.recordcount# neq 0>
<cfheader name="location" value="http://www.ourdomain.com#redirect.new_url#">
<cfheader statuscode="301" statustext="Moved Permanently" />
<meta http-equiv="refresh" content="5;url=<cfoutput>#redirect.new_url#</cfoutput>" />
<cfelse>
<cfheader statuscode="404" statustext="Not Found" />
</cfif>
Is this the proper way to do this sort of thing, or am I totally off-base?