PHP Code Snippet

Because of the re-design, there’s a lot of broken links – mainly because the blog used to live under the /whatithink/ folder on the server and now resides in the root folder. So all those Google links now point to a part of the site that no longer exists.

So anyone coming here from Google is going to get a 404 error. Not good.

While I get my head around using .htaccess to automatically rewrite the broken URLs, I’ve created a 404 page that will, (hopefully), correct the URL.

Here’s the PHP:

$requested = $_SERVER['REQUEST_URI'];$parts = explode('/', $requested);foreach ($parts as $part) {switch($part) {case ('whatithink'):break;case (''):break;default:$out .= '/' . $part;}}$out = 'http://' . $_SERVER['HTTP_HOST'] . $out;

Because all my broken URLs have an extra “/whatithink/” section, I can just break the requested URL into parts, and rebuild it without that section.