On Jan 8, 2010, at 4:13 PM, Yiannis wrote:

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?

Move this into your web server configuration.

For Apache it would be this (this wouldn't match foo.domain.com, but you can modify it so it will)

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

-philip
-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.


Reply via email to