Ensure Subdomain
an ActionDispatch extension to handle subdomain redirects
Let's say you believe that using the www subdomain is dumb.
Ensuring your rails app doesn't use this subdomain is pretty easy, but now it's easier.
# Gemfile
gem 'ensure_subdomain'
# Preferred terminal
bundle install
# config/routes.rb
Rails.application.routes.draw do
ensure_apex # or ensure_no_www or ensure_non_www
# The rest of my cool routes
end
$ curl -I http://www.example.com
HTTP/1.1 301 Moved Permanently
Location: http://example.com
Simple as that.
Conversely, if you do want to use the www subdomain, there's also a method for that.
# config/routes.rb
Rails.application.routes.draw do
ensure_www
end
$ curl -I http://example.com
HTTP/1.1 301 Moved Permanently
Location: http://www.example.com
There's also a method for whatever custom domain or domains you'd like.
# config/routes.rb
Rails.application.routes.draw do
# Single domain
# The only allowed subdomain is blog. All others will redirect.
ensure_subdomain 'blog'
end
$ curl -I http://example.com
HTTP/1.1 301 Moved Permanently
Location: http://blog.example.com
$ curl -I http://www.example.com
HTTP/1.1 301 Moved Permanently
Location: http://blog.example.com
# config/routes.rb
Rails.application.routes.draw do
# Multiple domains
# All of these subdomains are allowed. If another subdomain is requested, the
# response will redirect to en because it's first.
ensure_subdomains %w(en es fr jp)
end
$ curl -I http://jp.example.com
HTTP/1.1 200 OK
$ curl -I http://example.com
HTTP/1.1 301 Moved Permanently
Location: http://en.example.com
We've even got you covered if you want different rules for different environments.
# config/routes.rb
Rails.application.routes.draw do
ensure_on production: 'www',
staging: 'staging',
development: 'dev'
end
Please note that multiple rules don't work together well. It's recommended to just add a single ensure rule at the top of your routes.
Also recently added, and somewhat experimental, not fucking up on Heroku!
Before: GET application.herokuapp.com → 301 herokuapp.com → 301 heroku.com
After: GET application.herokuapp.com