PHP: Faking Send Headers for Friendly URLs CodeUnit 08 APR 2009

PHPNow as I’ve mentioned numerous times before, my main blog at runs of a file-based, heavily modified version of Simple PHP Blog, simply because at the time I was looking for a content publishing platform that didn’t require a database in order to function. Of course, this also means I don’t get all the nifty features that get jam-packed into popular platforms like WordPress or Blogger, but at least it does mean I get to stretch my coding fingers just that little.

Let’s look at ‘friendly’ URLs. Now if you visit a particular post on a WordPress hosted site, often you’ll see the URL given in its ‘friendly format’, i.e. usually in the format of domain/year/month/day/post-title/. Now these fake URLs are particularly important because they boost your search engine ranking for that particular post by including the title/topic/keyword in the URL, which obviously is quite fantastic if you’re a search engine whore. Also, it makes it much easier for the reader as well, because now one can quite literally at a glance, tell what any particular link might actually be about without actually clicking on it!

So then, how does someone like me achieve this as well? Well, if you go to the Cosplay Sundae site, you’ll notice that the direct post links are now in fact in friendly form while the links on the main CodeUnit blog aren’t. So how did I do this?

Well essentially what I now have is a lookup table for each blog entry (the ‘true URL’) that corresponds with it’s associated ‘fake URL’. When a browser hits a ‘fake URL’, the resulting error handler page (dictated by my ErrorDocument 404 /404.php line in the local .htaccess file), performs a quick lookup on the requested URL, and if it doesn’t find a match, then simply redirects to the appropriate ‘404 page not found’ location.

However, if it does find a match, I now spit out the contents of the corresponding ‘true URL’ page, most importantly remembering to set the headers before doing anything else using the PHP commands:

header(‘HTTP/1.1 200 OK’);
header(“Status: 200 OK”);

This is essential because instead of leaving them in their current error (404) status, you want to tell search engines and browsers alike that the page you are currently viewing is alive and well, and is in fact sitting at its correct address (even if it technically isn’t).

One of the easiest ways to ensure that you are indeed faking your headers correctly is to make use of the myriad of online header checking tools that are available on the Net. For example, SEO Image has a nice little tool on their site which you can access here. Essentially it gives you a full breakdown of the headers and content passed back from accessing your given URL, and if it given the thumbs up after processing is complete, then in all likelihood you’re looking good!

(Though just to be safe, it might be wise to submit your sitemap to Google and see whether they like it as well…)

Related Posts:

About Craig Lotter

Software developer, husband and dad to two little girls. Writer behind An Exploring South African. I don't have time for myself any more.