RSS

Recent Comments

Powered by Disqus
Mar
12th
Fri
permalink

301 redirect for base domain to www subdomain on Passenger + Apache2 + Rails

I can’t really remember all the reasons why its a bad idea to have both the base domain and the www subdomain point to the same rails app. I know sessions is one reason, if a user logs in on mydomain.com and then visits www.mydomain.com he is not logged in there. Consistency is also nice. I can’t be sure but it might even affect SEO.

So the best way I found to do this is to simply add another site in apache2 with the following:

<VirtualHost *:80>

ServerName mydomain.com

Redirect permanent / http://www.mydomain.com/

</VirtualHost>

And have the rails application only respond to www.mydomain.com

Other random notes

  • sudo a2dissite [sitename] - does not make changes to sites in /etc/apache2/sites-available 
  • you should not make changes to the vhost configs in sites-enabled, instead make changes to the configs in sites-available and then just do a sudo a2ensite even its already loaded, it will replace the old file. Then just reload apache to activate changes.

UPDATE: Found a good blog post with an alternative solution and more reasons to do this.

    blog comments powered by Disqus