Redirect non-www requests to www urls

In one of my recent projects, i’ve faced a situation where i need to redirect non-www traffic to a canonical www. For someone who don’t understand the issue, i want my users who type http://foo.bar in their browsers to be redirected to http://www.foo.bar. The tried and tested way to achieve this redirect is through server configuration.

I am using Apache as my web server with Passenger module installed for the website in question. Here is the step-by-step procedure on how i did it.

  1. Locate your apache configuration file. Usually it can be located at /etc/httpd/conf/httpd.conf (YMMV !) 
  2. Ensure that mod_rewrite is installed (which is the case with most hosting providers).
  3. Open the file using VI editor and switch to insert mode.
  4. Locate the VirtualHost directive specified for the website in question.
  5. Type the following rewrite rules inside the directive.
    <IfModule mod_rewrite.c>
      Options +FollowSymlinks
      RewriteEngine On
      RewriteCond %{HTTPS} !=on
      RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
      RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    </IfModule>
    
  6. Restart apache and you are done.
  7. Alternatively, you can create a .htaccess file at the root of your hosting and include the above rewrite rules in it.

The powerful Router in Rails 3 and above makes this trivial.

Foobar::Application.routes.draw do
  constraints(host: /^(?!www\.)/i) do
    match '(*any)' => redirect { |params, request|
      URI.parse(request.url).tap { |uri| uri.host = "www.#{uri.host}" }.to_s
    }
  end

  resource :foo_bar
  root :to => redirect('/foo_bar')
end
Foobar::Application.routes.draw do
  constraints(host: /^www\./i) do
    match '(*any)' => redirect { |params, request|
      URI.parse(request.url).tap { |uri| uri.host.sub!(/^www\./i, '') }.to_s
    }
  end

  resource :foo_bar
  root :to => redirect('/foo_bar')
end

Leave a comment