Tuesday, October 18, 2011

Blogger - Redirect when you change Blog URL


Recently I changed URL of my blog from giorgioguerrieri.blogspot.com to webdevshareetc.blogspot.com.
I know that my posts linked in facebook, google+, twitter have the old URL, so I search for a solution, a way to redirect post giorgioguerrieri.blogspot.com/2010/eccccc to webdevshareetc.blogspot.com/2010/eccccc, without 404 or other annoying message of Page Not Found.
So, I googled but nothing.

I found my solution with this process.
NOTE: it works if you have a personal site and it's used not for personal site. I use my site for development, so when you navigate to www.giorgioguerrieri.it your browser redirect to my blog.

You have change URL of your blog (giorgioguerrieri to webdevshareetc.blogspot.com)

Create a new blog with URL equals to old URL (new blog giorgioguerrieri.blogspot.com)

Go to Settings in new Blog and set Redirect to your public domain (I've www.giorgioguerrieri.it).

Important! You have to insert URL as "www.yourdomain.com?", yes, with "?".

Save settings and test: when you navigate to your new blog (with old URL), you navigate to your site.

Now, go to ftp of your site and modify index.php (I use php, but you can do this with your language).

function curPageURL() {
 $pageURL = 'http';
 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
 $pageURL .= "://";
 if ($_SERVER["SERVER_PORT"] != "80") {
  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
 } else {
  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
 }
 return $pageURL;
}

  $urlpage = curPageURL();
  $pos = strpos($urlpage, '?/');
  if($pos > 0)
  {
  $urlpage = substr($urlpage, $pos+1);
  $urlpage = "http://webdevshareetc.blogspot.com" . $urlpage;
  }

header("location: " . $urlpage);


If you navigate to "http://blog.blogspot.com/2010/10/post.html", blogger asks you to redirect to "http://www.yourdomain.com?/2010/10/post.html".
Note the "?/...."
Then your index.php takes URL, cut from "?" and compose URL with "http://newurlblo.blogspot.com" + "/2010/10/post.html".
Then redirect (header("location")) to new URL correct page!

You can do other redirect from index.php simply checking if exists "?/" or not in the received URL.
In my script, if there's no "?/", I redirect to home of new Blog.

I hope this help you!