I’m hosting a domain with both addresses http://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 http://www.domain.com to domain.com as well as http://www.domain.com/something to domain.com/something.
I’m just wondering on best practice for redirecting all traffic from http://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.
Refer here: http://httpd.apache.org/docs/2.2/rewrite/remapping.html#canonicalhost