Aug 28 2012

Redirect www Subdomain to Your Apex Domain Using the Rails Router

No www. - using the Rails Router

It’s 2012. We know it’s the World Wide Web. We don’t need to be reminded of that by prepending www. to the URL of every site we visit. In fact, the use of www. as the de facto subdomain of The Web was an accident. And what’s more, it was deprecated back in August of 2003.

I am over www. and hereby pledege to do my best to stop using it on the portions of the Internet that I build. To be successful, I need the Ruby on Rails-powered apps I create to also drop the dubs. The powerful router in Rails 3 (and newer) makes this trivial. Peep this:

Let’s talk about what’s going on there…


Routing Constraints

That outermost constraints block is a Rails routing constraint that only matches requests where the host starts with www.. When that constraint is met, we match ALL THE PATHS and any HTTP verb. Upon making the match, we tell the router to redirect the request to a new URL.

The new URL is whatever URL the request came in on, sans the leading www..

Easy peasy!

What if you want the dub-dub-dub?

For starters, I’ll hold it against you. But then I’ll immediately get over it and we’ll go back to being friends.

In fact, as a token of my friendship, I’ll show you how to redirect non-www. requests to a www.-version.

This works much like the first example, but in reverse. An incoming request lacking a leading www. matches our constraint and gets redirected to the same URL prefixed with a www..

Also easy, just more dub-dubs.

To www. or not?

Do you have a preference or just an argument? Let’s hear it!

4 Comments

  1. Alex Heeton

    What are you thoughts on using this approach with cloud-based hosting like Heroku or EC2?

    Their own guidelines warn against using Apex/Naked domains: Seems mainly because the only type of DNS record allowed is a simple A record, pointing to a fixed IP address. With cloud providers, that IP shouldn’t be fixed (to allow for changing servers when needed).

    • Alex Heeton

      I should have said “What are you thoughts on using this approach given the prevalence of cloud-based hosting like Heroku or EC2?”

    • Hello Alex,
      Apex domains are an issue for a lot of cloud-based hosting due to the way CNAME and A DNS records work. However, there is a new, and hopefully emerging, record type that intelligently handles this: ALIAS.

      That I know of, only two DNS providers have implemented this record type (so far) – my friends at DNSimple.com and Amazon’s Route 53.

      I’m actually using ALIAS records for my personal blog, which is hosted on Heroku, and it’s been working great for months!

      Have a quick Google search and investigate ALIAS records, and then let me know what you think!

      • A quick note on Route 53.

        According to Heroku’s docs, Route 53 does not support ALIAS records generally. However, they do use them for S3 Buckets and Elastic Load Balancer.

        If you do find a DNS provider that does support ALIAS please leave a comment here and let us know! Until then, your best bet looks to be DNSimple.com – a service I :heart:.

Leave a Comment

Join the discussion. Do not worry, your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>