I want my rails app to redirect from no-WWW to WWW domain (for example
if I enter example.com/test to redirect to www.example.com/test). I
put the following code in the application controller and worked fine:
before_filter :check_uri
def check_uri
if !/^www/.match(request.host)
redirect_to request.protocol + "www." + request.host_with_port +
request.request_uri, :status => 301
end
end
But I also do a simple pages_cache and the above code doesn't execute
when the page is cache. Is any other way to call the above method from
route.rb even when the pages are cached?
Unfortunately I am using fastCGI to deploy and my hosting doesn't
support custom httpd.conf files. I think the only way is within rails
or with .htaccess.
Thank you all for the help, I finally make it work as far as I test it
(even though I don't totally understand how it works). The htaccess is
the following in case someone else need it: