Performing 301 Redirection via a Custom 404 Handler

Performing 301 Redirection via a Custom 404 Handler

ASP Method

Using the below ASP method is recommended as it works best with our IIS-based servers to serve the appropriate redirections, response codes, and content to be both SEO and visitor-friendly.


<%
strRequest = lcase(Request.Querystring)
strRequest = Mid(strRequest, InStr(strRequest, "://") + 3)
strRequest = Mid(strRequest, InStr(strRequest, "/"))

Select Case strRequest
  Case "/oldpath/old_name.html"
    Response.Redirect "/newpath/newname.asp"
  Case "/oldfile.html"
    Response.Redirect "/newfile.php"
  Case "/subsite/index.html"
    Response.Redirect "http://subsite.mainsite.com/index.html"
  Case Else
    Response.Status = "404 Not Found"
End Select
%>
<html>
<head>
	<title>404 Page not Found<title>
</head>
<body>
	<h1>404 Page not Found</h1>
	<p>The page you've requested was not found.</p>
	<!-- Internet Explorer does not properly display error pages under
		512 bytes, so here are 512 bytes of filler text!

		Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi
		accumsan pretium lorem, eget laoreet nisl accumsan eget. Vivamus
		molestie tempor aliquet. Sed pellentesque laoreet felis interdum
		congue. Curabitur scelerisque rutrum quam. Nunc sed dui turpis,
		ornare eleifend mi. Duis bibendum neque ac massa laoreet venenatis.
		Vivamus tempor luctus nisl vitae molestie. Ut iaculis orci ac elit
		commodo malesuada. In neque risus, blandit eu fringilla id,
		ultricies ac risus. Nullam enim dolor, bibendum mollis amet. -->
</body>
</html>


PHP Method

The below PHP method has the drawback of either being able to issue a 404 response with no content, or a 200 response with content. This is due to the way IIS interacts with PHP running as CGI. As noted in the comments for this file, you must choose between a visitor-friendly 200 with content, or an SEO-friendly 404 with no content.


<?php

$request = substr($_SERVER['QUERY_STRING'], strpos($_SERVER['QUERY_STRING'], '://')+3);
$request = substr($request, strpos($request,  '/'));

$arr_move = array(
	"/oldfile.html"		=> "/newfile.html",
	"/folder/nothere.php"	=> "/new_path/newname.php"
);
           
if(array_key_exists($request, $arr_move)) {
	$redirect = "http://" . $_SERVER['HTTP_HOST'] . $arr_moved[$request];
	header("HTTP/1.0 301 Moved Permanently");
	header("Location: $redirect");
	header("Connection: close");
	exit();
}

// PHP running as CGI in IIS does not allow for any content to be returned with a
// 404 status code. If you wish for the trailing content to be returned under a
// 200 OK status leave the  below commented out. If the page MUST issue a 404 response
// [ie. for SEO purposes] then uncomment this section, but be aware that all 404 pages
// served to a client browser will be blank.

/*
else {
	header("HTTP/1.0 404 Not Found");
}
*/

?>
<html>
<head>
	<title>404 Page not Found<title>
</head>
<body>
	<h1>404 Page not Found</h1>
	<p>The page you've requested was not found.</p>
	<!-- Internet Explorer does not properly display error pages under
		512 bytes, so here are 512 bytes of filler text!

		Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi
		accumsan pretium lorem, eget laoreet nisl accumsan eget. Vivamus
		molestie tempor aliquet. Sed pellentesque laoreet felis interdum
		congue. Curabitur scelerisque rutrum quam. Nunc sed dui turpis,
		ornare eleifend mi. Duis bibendum neque ac massa laoreet venenatis.
		Vivamus tempor luctus nisl vitae molestie. Ut iaculis orci ac elit
		commodo malesuada. In neque risus, blandit eu fringilla id,
		ultricies ac risus. Nullam enim dolor, bibendum mollis amet. -->
</body>
</html>

Please note that the above code is provided as a courtesy by Alentus, and is presented with neither guarantee or warranty.

Add Feedback