URL rewrite tips and solutions
Friday, December 7th, 2007Due to server setup and having no access to the php.ini file to change your server settings, we will resume to the .htaccess file for rewriting the URL, removing or adding the www. and removing the PHPSESSID from the URL itself to prevent duplicated content into the Search Engines, so here are few tips to get you started:
First lets create a new file and name it .htaccess (only if you don’t already have one), and ad the following lines at the very top:
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
Now we are going to check the URL making sure the www. is always there otherwise we will redirect to the right format URL:
# redirect if not www
RewriteCond %{HTTP_HOST} !^(www)\.domain-name\.ie
RewriteRule ^(.*)$ http://www.domain-name.ie/$1 [R=301,L]
Now lets redirect the index.php or .html to the main URL as this creates duplicated content as well:
#redirect index.php or htm to /
RewriteCond %{THE_REQUEST} ^GET\ .*/index\.(htm|html|php)\ HTTP
RewriteRule ^(.*)index\.(htm|html|php)$ /$1 [R=301,L]
Depending on your server settings, sometimes the PHPSESSID get attached to the end of your URL, which will also created duplicated content, so there is a solution written in php that will check for the PHPSESSID query-string and if found, we will remove it and do a 301 Permanent Redirect to the same page but without it:
//remove session id from url
if (!empty($_GET['PHPSESSID'])) {
$clean_uri = preg_replace('/?PHPSESSID=[^&]+/','',$_SERVER['REQUEST_URI']);
$clean_uri = preg_replace('/&PHPSESSID=[^&]+/','',$clean_uri );
header('HTTP/1.1 301 Moved Permanently');
header('Location: http://'.$_SERVER['HTTP_HOST'] . $clean_uri );
exit();
}
//
Please Note: The above solution is not part of the .htaccess file, but has to be added to a common include file available across the site.














