Unfortunately, the concept of "index" pages (index.php, Default.asp, etc.), a feature since the beginning of time on the web, causes yet another duplicate content problem.  If no file name in a directory is provided to a webserver, the "index" page is typically provided by default, but without redirection. The problem arises when both URLs are linked, either internally or from other sites.  Bang - duplicate content.  Strictly speaking, neither URL is more correct, though shorter URLs are usually desirable, and hence "/" is favored over "/index.php." The word "index.php" really doesn't add any useful information anyway.

This is a similar problem to the www.domain.com vs domain.com issue, and though Google resolved to fix both issues with the "BigDaddy" update, it is still an issue (though perhaps less of one), and I will show here how to remove this ambiguity entirely. 

This sample code shows how to use a function "assertProperURL()" in coordination with a global variable (somewhere in your header or config file, hopefully) that stores the site's main domain URL.  This can also be used to assert that any page has a certain URL in order to correct them.

<?php

// NOTE:no trailing /
$GLOBALS['site_base_url'] = 'http://www.foo.com';

function assertProperURL($proper_url)
{
  global 
$site_base_url;

  $request_uri $_SERVER['REQUEST_URI'];

  $current_url $site_base_url $request_uri;
  if (
$current_url != $proper_url) {
    
header("HTTP/1.1 301 Moved Permanently");
    
header('Location: ' $proper_url);
  }
}

assertProperURL($GLOBALS['site_base_url'] . '/'true);

?>

As a test, try to access http://www.lawyerseek.com/index.php; I dare you :) 

Tell an amigo:
  • Sphinn
  • Digg
  • Reddit
  • del.icio.us
  • StumbleUpon
  • Facebook



Related posts:
ROTD: Mod_rewrite Rule To Remove Trailing "index.php" This handy set of rules for mod_rewrite automatically redirects any...
URL Normalization; Slashing Duplicate Content To be honest, I'm not even sure this matters much...
Does URL Parameter Order Matter? This is a question I'm currently researching. I have never...
SSH2 & PHP Howto Guide: SSH Connections Made Easy in PHP PHP doesn't come with native support for making SSH connections...
SimpleCloak v2 PHP Implementation Note: I didn't realize WP changes quotes to curly quotes...