Skip to content

Instantly share code, notes, and snippets.

@khiet
Last active July 8, 2020 09:16
Show Gist options
  • Save khiet/35f9381fac71414268c004cd89549567 to your computer and use it in GitHub Desktop.
Save khiet/35f9381fac71414268c004cd89549567 to your computer and use it in GitHub Desktop.
Rails 5 redirect root domain to www and vice versa
# example.com => www.example.com
match '(*any)', to: redirect(subdomain: 'www'), via: :all, constraints: { subdomain: false }
# www.example.com => example.com
match '(*any)', to: redirect(subdomain: false), via: :all, constraints: {subdomain: 'www'}
# caveat
# subdomain is anything from 3rd level domain, so for example.co.uk domains the syntax will be the following:
# example.co.uk => www.example.co.uk
match '(*any)', to: redirect(subdomain: 'www.example'), via: :all, constraints: { subdomain: 'example' }
# www.example.co.uk => example.co.uk
match '(*any)', to: redirect(subdomain: 'example'), via: :all, constraints: { subdomain: 'www.example' }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment