Redirecting With PHP & htaccess

This blog used to be at the url http://pdwd.net/blog/. When I moved it over to it’s own sub-domain, I still got a few people going to the old domain looking for the blog, so I needed to redirect them. Here was my code.

My .htaccess file looked like this:

ErrorDocument 404 /404-redirect.php

The 404-redirect.php file looked like this:

<?php if (strpos(" ".$_SERVER['REQUEST_URI'], "/blog/")) {
	header("Location: http://blog.pdwd.net");
} elseif (strpos(" ".$_SERVER['REQUEST_URI'], "/blog")) {
	header("Location: http://blog.pdwd.net");
} else {
	header("/");
} ?>

The methodology was that if people entered the old URl, they would automatically be sent to the new one, but some people added the / at the end and some didn’t, so I added both into the mix. Sorted.