Fix Your Permanent Redirect To Optimize Your Blog SEO

The following website URL’s may seem like they are the same page to you, but to a search engine they might actually seem like two different pages:

http://yourblog.com

http://www.yourblog.com

Search engines may think www.yourblog.com and yourblog.com are two different blog sites. You should set up a permanent redirect (technically called a “301 redirect”) between these sites. Once you do that, you will get full search engine credit for your work on these blog sites and it will improve your search engine optimization and the SEO rankings.

A simple edit in your index.php file in your blog root directory will fix the permanent redirect for you. This is the text you need to paste into your index.php (replace “yourblog” with your blog URL without .com). So for http://www.yourblog.com you would only write yourblog as seen below:

// non-www redirect
if (strstr($_SERVER['SCRIPT_URI'], “http://yourblog”) !== false) {
$newLocation = str_replace(“http://yourblog”, “http://www.yourblog”, $_SERVER['SCRIPT_URI']);
header(“HTTP/1.1 301 Moved Permanently”);
header(“Location: “.$newLocation);
}

When this is saved to your index.php, all your visitors will be redirected to www.yourblog.com even if they enter yourblog.com in their browser address bar.

As I have mentioned before you should add your blog to Google Webmaster Tools. There you can confirm this permanent redirect directly with Google.

Login to your Google Webmaster Tools account, go into “Tools” and choose “Set Preferred Domain”. There you can associate a preferred domain with your blog site, to always or never show the leading “www.” in Google’s search results.

Join thousands of bloggers and get all my blogging tips for FREE! Subscribe to HowToMakeMyBlog via RSS or via e-mail.

If you liked this article, you may also like:

  • Why You Should Add Your Blog To Google Webmaster Tools
  • Improve Blog SEO With Internal Linking Structure
  • 8 additional optimization steps for your WordPress blog
  • Do not worry about SEO, just concentrate on your blog readers
  • How to choose a domain name for your blog
  • Why I run my blog on Thesis Wordpress Theme

    WordPress Thesis Theme

    Thesis theme gives my blog a professional, clean, easy-to-read layout and SEO friendly design. Thesis makes it simple to make your own blog unique. See more details and get your own Thesis today.

    Post written by Marko Saric on November 2, 2008 in How To Set Up A Blog

    { 7 comments }

    Paul J Roberts January 3, 2009

    Marko,

    I used this script with no issues on my Blue Grace template. Then when the author updated the theme to vs 3.0 (and I did too), now anywhere i put the script in the index.php…it shows up on the web page. If I move it in somewhere else in the index.php….it shows up in the page in that area. Any suggestions?
    Thanks.

    Marko January 3, 2009

    @ Paul – I am not sure what the problem is. Try to put something like this into your index.php:

    < ?php
    if (substr($_SERVER['HTTP_HOST'],0,3) != ‘www’) {
    header(’HTTP/1.1 301 Moved Permanently’);
    header(’Location: http://www.’.$_SERVER'HTTP_HOST']
    .$_SERVER['REQUEST_URI']);
    }
    ?>

    Paul J Roberts January 4, 2009

    Marko,

    Ok, I see your PHP open/close tags are different now and match the Index.php tags. But, where in your script do I place my URL? I’m a bit of a php noob.

    Thanks

    Marko January 4, 2009

    @Paul – In that one you do not need your URL. I am not either, I had my script done from a friend, so I was looking for “301 redirect wordpress” on Google now for you.

    Paul J Roberts January 4, 2009

    Ok, thanks. You did not have to do that…I could have done it. LOL. I just thought I’d check the source. I’ll give it a try and see what happens.

    Thanks again.

    technewsbeats February 6, 2009

    Thanks for the easily understandable article.

    Tom March 2, 2009

    Thanks Marko for pointing out the importance of this issue. I have been aware of it, but never paid much attention. Though I run Drupal, I have researched how to do redirect, and fixed the issue in two minutes.

    Previous post:

    Next post:

    Search Enginedata recovery