SERP correct domain 302 redirect website solution

A lot of talk in the SEO world on what to do with the problems that search engines have with the same site (page) having listed multiple url versions in the SERPS. For example:

  • https://yvoschaap.com
  • https://yvoschaap.com
  • https://yvoschaap.com/index.php

Could all be viewed as different pages by search engines, while it’s not. This resulted for some sites in a “duplicate content” penalty. I advise you guys to read more about this here or here.

My general solution to this is this simple and small piece of PHP. So NO htaccess stuff, or manual redirecting, etc.

$domain = "www.yvoschaap.com";
if($_SERVER['HTTP_HOST'] != $domain || preg_match("#index.php#i",$_SERVER['HTTP_HOST'])){
  header("HTTP/1.1 301 Moved Permanently");
  header("Location: http://".$domain.$_SERVER['REQUEST_URI']);

It says this: if host isn’t the “www” version OR if it is the /index.php version. Show an official “moved” page, with the right location: https://yvoschaap.com/
Just put it in the top of your index.php file, et voila. No rocket science…

Posted by on .