TheDrunkenEpic - Drunken Ramblings of a Code Monkey
« Back to blog

Domain Redirects Using PHP for SEO Optimization!

Hello, everyone! It's been a while since I wrote a nice PHP tutorial, so here I am... doing just that! Today's article will deal with something I'm pretty sure you've all seen while on the web. How many times have you visited your favorite website by manually typing in the domain name in the address bar? Yeah. Lots. Sometimes you add the 'www.' and sometimes you just jump straight into the meaty part and type 2girls1... err... and the browser does the rest for you.

Ever wonder how this happens and, more importantly, why you should take this into consideration for your websites? Well, it's done using 301 header and canonical redirects. Let's get to the details and discuss the whys and hows.

But... Why?

While 'thedrunkenepic.com' and 'www.thedrunkenepic.com' might take you to the same place, from an SEO standpoint they're very different. Search engines may take those two urls and interpret them as different sites which could screw up your search engine ranking in some cases. Also, if you're using a web metrics service like Google Analytics, your reports, especially custom ones, might be spread across what appears to be two different sites as well. While this may not be a HUGE deal, it does add another layer of complexity to tracking visitors and other trends on your site, which is a chore in and of itself.

How Do I address problem?

Simple. By doing a '301 Permanent Redirect' from 'thedrunkenepic.com' to 'www.thedrunkenepic.com'. By doing this a visitor who clicks on or enters your site's link with no sub domain will be taken straight to the 'www.' version. Your analytics software will understand the '301' server response and act accordingly AND search engines will also make note of this next time they spider links to your site.

Simple? Surely you jest, good sir!

This isn't a standard header redirect, the 301 server response tells browser and search engines that the page you're accessing has been permanently moved to the following location. As far as metrics software and spiders are concerned 'thedrunkenepic.com' and 'www.thedrunkenepic.com' are now the same.

TEACH ME HOW!!!1one

Well, as the administrator of your website there are a few ways to take care of this issue. A solution is to use an .htaccess file with special mod_rewrite directives to redirect all requests that do not point to 'www.thedrunkenepic.com' to 'www.thedrunkenepic.com'. But, we're not going to get into this solution today as I prefer a lighter approach, one that makes use of my coding language of choice and isn't anywhere near as intensive as mod_rewrite; PHP.

Unlike .htaccess, this wouldn't be a global solution that works across your entire site, it sort of depends on your platform that serves your content. For the sake of of this article, we'll just assume all content for your site is filtered through a single landing page in the form of 'index.php' located in your public root directory.

Example: http://www.thedrunkenepic.com/index.php

Since the DirectoryIndex directive on most Apache servers, by default, contains 'index.php', this is your site's default landing page. But, you already knew that, didn't you?

This is beginning to sound a bit too complex for me...

Alright, now we're getting to the good stuff. We have all requests for our site starting at 'index.php'. Now we can determine what and what not to redirect.

First, we get the client requested host name by using $_SERVER['HTTP_HOST'] and break it down to manageable bits:

<?php

$host_bits = explode('.', $_SERVER['HTTP_HOST']);

Next, the script attempts to determine the current HTTP_HOST's sub domain(s) and then rebuild the domain's root:

<?php

$host_bits = explode('.', $_SERVER['HTTP_HOST']);

if(sizeof($host_bits) > 1)
{
    $tld = array_pop($host_bits);
    $domain = array_pop($host_bits) . ".{$tld}";
}
else
{
    $domain = array_pop($host_bits);
}

By using array_pop() we are iterating through the pieces of the domain, one by one. By this point, the bits of the host name that we have left, if any, are the actual sub domains. Since your site may actual make use of valid sub domains, we're only going to redirect if $host_bits is empty. In other words, if nothing is left in $host_bits and we have our actual root $domain, it's pretty safe to assume that the current user is attempting to access the site without the 'www.'.

If this is the case, we do a server-friendly redirect using the appropriate headers and exiting our script.

<?php

$host_bits = explode('.', $_SERVER['HTTP_HOST']);

if(sizeof($host_bits) > 1)
{
    $tld = array_pop($host_bits);
    $domain = array_pop($host_bits) . ".{$tld}";
}
else
{
    $domain = array_pop($host_bits);
}

if(false == implode('.', $host_bits))
{
    header("HTTP/1.1 301 Moved Permanently");
    header("Location: http://www.{$domain}{$_SERVER['REQUEST_URI']}");

    exit();
}

Note, that when we're invoking the Location header, we're adding $_SERVER['REQUEST_URI'] to the end of the domain name. This allows the script to preserve any extras at the end of the original URL. This prevents stuff like sub directory or file requests from being lost.

In Conclusion

Well, this article is a little longer than I wanted it to be, but I hope I explained this common problem well enough for you to understand its implications. Not only that, you now know how to tackle and resolve this issue. Naturally, if you have any questions about the article, don't hesitate to ask using the comment form below.

The above code should work on any server. To make it more efficient, though, you might want to wrap it up in a function and use it where ever it's needed in your site and other scripts. Also, just because it's in PHP doesn't mean you can't port it over to ASP or other languages. The code is simple enough to break down and translate and I'll always be more than happy to help you out if needed.

Also, if there is an interest, I'd be more than happy to post the Apache mod_rewrite solution. Although it'd work globally throughout your site and it wouldn't have to rely on PHP, it is a bit more server intensive.

I hope you enjoyed reading this article as much as I enjoyed writing it.

Cheers!

Note: This article as been republished after being accidentally wiped from my hosting provider. Hurray for cached RSS feeds!

Loading mentions Retweet

Comments (0)

Leave a comment...

 
To leave a comment on this posterous, please login by clicking one of the following.
Posterous-login     twitter