Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force www. in rails

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?

like image 207
Chris Avatar asked Oct 30 '25 08:10

Chris


2 Answers

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.

like image 179
khelll Avatar answered Nov 01 '25 07:11

khelll


If you're on a newer Rails version, an alternative to using Apaches mod_rewrite would be using the Canonical Host Rack middleware.

like image 42
Jakob S Avatar answered Nov 01 '25 05:11

Jakob S



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!