I'm hosting a domain with both addresses www.domain.com and domain.com through an "alias" directive specified in the VirtualHost config file:
<VirtualHost *:80>
ServerName domain.com
ServerAlias www.domain.com
DocumentRoot /var/www/domain.com/current/public/
<Directory /var/www/domain.com/current/public/>
AllowOverride all
Options -MultiViews
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Now http://www.domain.com and http://domain.com go to the same content. I need to redirect every request for www.domain.com to domain.com as well as www.domain.com/something to domain.com/something.
I'm just wondering on best practice for redirecting all traffic from www.domain.com to domain.com. Is should be done at Apache, Passenger or Rails level? and how? 301 redirect?
Any ideas appreciated. Thank you.
EDIT:
The working configuration is now this one (with "Redirect permanent"):
<VirtualHost *:80>
ServerName www.domain.com
Redirect permanent / http://domain.com/
</VirtualHost>
<VirtualHost *:80>
ServerName domain.com
DocumentRoot /var/www/domain.com/current/public/
<Directory /var/www/domain.com/current/public/>
LimitRequestBody 4096000
AllowOverride all
Options -MultiViews
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
The very best way to solve this is by using Redirect directive placed in a virtual hosts.
<VirtualHost *:80>
ServerName www.example.com
Redirect / http://example.com/
</VirtualHost>
<VirtualHost *:80>
ServerName example.com
</VirtualHost>
Refer here: http://httpd.apache.org/docs/2.2/rewrite/remapping.html#canonicalhost