CUBISTCODE

CUBISTCODE

No index.php

Sometimes when users navigate your web site or type in URLs they will see http://yoursite.com/index.php in their browser's address line. This can reduce the impact of any SEO techniques you use on that page and cause things like your Facebook 'Like' count and Twitter tweet button count to become spread over two URLs when they are really both the same.

 Use this bit of PHP code to stop this from happening by redirecting visitors to your domain root (just your domain ending in a slash) on page load:

// redirect user's browser if called as /index.php
if ($_SERVER['REQUEST_URI'] == $_SERVER['SCRIPT_NAME']) {
    header('Location:/');
    die;
}

How it works:
The code first tests if the URI the script was called at equals the script's filename on the server. If it does, the second line executes the redirect sending an HTTP Location: header to the user's browser.

The last line is very important, since it's what causes the script to exit after the redirect. Without the die() statement, the remainder of your index.php will continue to execute on the server after redirecting the user's browser! Since this code is meant to be inserted near the top of your index.php file, that means of your site will still be running, potentially tying up database connections, network and file resources until they eventually time out.

 



Tags: php, web

Rate This Article:



Privacy Policy | Copyright/Trademark Notification