Archive for drupal

Redirect All Users to Access Your Site WITH/WITHOUT ‘www.’ Prefix

Posted in lamp with tags , , , on February 12, 2009 by Salimane Adjao Moustapha

The basic idea here is when the user enter salimane.com, they get redirected to www.salimane.com . Some people prefer the opposite. In the default drupal .htaccess  you have the following :

# To redirect all users to access the site WITH the 'www.' prefix,
# (http://example.com/... will be redirected to http://www.example.com/...)
# adapt and uncomment the following:
# RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
# RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
#
# To redirect all users to access the site WITHOUT the 'www.' prefix,
# (http://www.example.com/... will be redirected to http://example.com/...)
# uncomment and adapt the following:
# RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
# RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]

Let me tell you straight, it doesn’t work . The working solution that i found is to have two virtual hosts :
- http://example.com/
- http://www.example.com/

and the former permanently redirect to the later or vice versa.  You can have one apache virtual host configuration  that looks like this for the site : salimane.com

	<VirtualHost *:80>
	       ServerAdmin webmaster@localhost
	       ServerName salimane.com
	       Redirect permanent / http://www.salimane.com
        </VirtualHost>

        <VirtualHost *:80>
	       ServerAdmin webmaster@localhost
	       ServerName www.salimane.com
	       LogLevel warn
	       ErrorLog /var/log/apache2/salimane.com.error.log
	       CustomLog /var/log/apache2/salimane.com.access.log combined
	       DocumentRoot /var/www/salimane.com/
	       <Directory /var/www/salimane.com/>
		      Options Indexes FollowSymLinks MultiViews
		      AllowOverride All
		      Order allow,deny
		      allow from all
	       </Directory>
        </VirtualHost>

Of course, you have to customise that to your settings.

Update : Another solution is to use CNAME as mentioned by Lukas in the comments.  CNAME records are domain name aliases. It can be used to provide access to a web-server using both the standard “www.salimane.com” and “salimane.com”.
This is usually done by creating an A-record for the short name (without www), and a CNAME-record for the www name pointing to the short name. By the way, A CNAME-record should always point to an A-record to avoid circular references.
you can have something like this :

; zone fragment for salimane.com
$TTL 2d ; zone default = 2 days or 172800 seconds
$ORIGIN salimane.com.
....
salimane.com   IN      A      192.168.0.3
www        	IN      CNAME  salimane.com