Search code examples
ruby-on-railsruby-on-rails-3routescanonical-link

Canonicalization in Rails - routing or .htaccess?


I have a site that is about to launch and a request for URL Canonicalization has been made. I want to know what is the best way to have all requests for http://www.example.com to permanently redirect (301) to http://example.com within my RoR app? Or, asked another way, how can I strip the "www." from all generated urls, paths, requests?

FYI, this is a Rails 3 app.


Solution

  • For Apache, You can add the code below to your /public/.htaccess file in your ROR app. I use this for most of my apps, because I don't like the 'www'

    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
    RewriteRule ^(.*) http://%1/$1 [R=301,L]
    

    Hope this helps