I'm normally used to using .htaccess files to force a domain to use www. (i.e. http://www.example.com instead of http://example.com):
#Options +Indexes
RewriteEngine on
RewriteBase /
Redirect permanent "www.example.com" "http://www.example.com"
Redirect permanent "example.com" "http://www.example.com"
However this doesn't work in a rails app. What is the rails alternative to this?
Check this
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(example\.com)(:80)? [NC]
RewriteRule ^(.*) http://www.example.com/$1 [R=301,L]
order deny,allow
Taken from http://www.htaccesseditor.com/en.shtml#a_WWW
Note: The .htaccess file should be put inside the public folder of the Rails project.
If you're on a newer Rails version, an alternative to using Apaches mod_rewrite would be using the Canonical Host Rack middleware.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With