Removing www. from your websites

Here is a short tip on how to remove “www.” from your sites (and make sure others do not use it too). You will need mod_php (apache) enabled and PHP.

In the default virtual host base directory, make sure you have enabled mod_rewrite (check your Override directives if the following does not work), and add something like this in .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule ^(.*) go.php

Then put in the same directory go.php:
< ?
$g = str_replace('www.','',$_SERVER['HTTP_HOST']) ;
header("Location: http://$g". $_SERVER["REQUEST_URI"]) ;
?>

2 Responses to Removing www. from your websites

  1. Check my solution here:
    http://rapidsignal.com/blog/2005/10/24/removing-www/

    This way you don’t need a php file plus redirection keep the full url
    e.g.
    http://www.site.com/path/to/file.php?par=1
    will send you to
    http://site.com/path/to/file.php?par=1

  2. Panayotis says:

    Yes, but I have multiple domains. I could not find a way to use the domain name in the rewrite rule.