The Coder

Remove WWW's & Add Trailing Slashes

Original posted by: Admin on 03-02-2007

There is a little tutorial posted in the PHP Miscellaneous section that show you how to remove the WWW's from your address via PHP.

However this isn't a very good solution on a large site so this short tutorial will show you how to remove them with the .htaccess file.

The .htaccess file should be placed in the public_html folder (or equivalent root folder), with a few lines of code you can abolish those nasty WWW's.


#Turn the rewrite engine on
RewriteEngine on
#Check that they are viewing the WWW version of your site
RewriteCond %{HTTP_HOST} ^www.(.+)$
#Make a redirect to the new address
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]


Pretty simple huh?

Another thing that can bother people when they are using the Mod Rewrite function is trailing slashes. If you notice all the links on thecoder website have trailing slashes, this is quite handy and was done with a simple line of code in the htaccess file


#Redirect user if no trailing slash
RewriteRule ^([^.]+[^/])$ http://%{HTTP_HOST}/$1/ [R=302,L]


Now your site won't have duplicate pages and you won't get penalized by the nasty search engine people.




Login to make a comment 0 Comments Found