Permanent redirect (HTTP 301) to specific domain while preserving querystring.
Problem
When you’re using several domains which all point to one website, it’s important to make sure that Google only sees on domain when it’s crawling your site for content. Otherwise you’ll likely end up with ‘suplemental result’ listings in the SERPS. At worst they’ll dump the site thinking it’s duplicating content.
A Solution
So what you need is something to intelligently redirect traffic to your preferred domain, while preserving any path and querystring data also. To do this with PHP you can place the following code (in an include maybe?) into the head of each page on the site:
<?php
$primary = “www.whatever.com”;
if ($_SERVER['HTTP_HOST']!= $primary) {
header(“HTTP/1.1 301 Moved Permanently”);
header(“Location: http://$primary”
.(($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : “?”
.$_SERVER['QUERY_STRING']));
exit;;
}
?>
This should redirect the page to the new domain, but preserve the path, query and any fragaments. Please be aware that not all servers send the complete set of $_SERVER Variables. Using the code above, if the HTTP_HOST variable isn’t present, no redirecting will be done. If the REQUEST_URI isn’t present, the QUERY_STRING will be tried. If this doesn’t exist the client will be redirected to the root of the specified domain.
A note about redirects: In this case, it’s really important that we send a 301 Moved Permanently response status code to the visiting client. If the visitor is a search bot, it ‘should’ update any references to the other domain in its index.
If you have any questions, comments for corrections, please post below!








Add to My Yahoo!
Leave your response!