Redirect 301 and cache

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?

Thank you

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

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

Thank you very much philip, I have also tried this but it seems I have
a problem with my dispatch.cgi (I am using fastcgi to deploy).

When I am trying I get this error
"Routing Error

No route matches “/dispatch.fcgi” with {:method=>:get}"

Here is my full .htaccess:

AddHandler fcgid-script .fcgi
AddHandler cgi-script .cgi
Options +FollowSymLinks +ExecCGI

RewriteEngine On
RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]

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

ErrorDocument 500 /500.html

#ErrorDocument 500 "

Any other suggestions? Thank you

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.

On Fri, Jan 8, 2010 at 4:13 PM, Yiannis [email protected] wrote:

   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?

Thank you

Yiannis, I have always been able to do this type of configuration within
my
hosting service without touching a .htaccess or httpd.conf files.
However,
you should be able to do the following for a named virtual host:

<VirtualHost *:80>
ServerName www.example.com
ServerAlias example.com *.example.com
DocumentRoot /path/to/rails/app/public

I take it that you’re using Passenger but it’s not clear from your
original
post.

Good luck,

-Conrad

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:

AddHandler fcgid-script .fcgi
AddHandler cgi-script .cgi
Options +FollowSymLinks +ExecCGI

RewriteEngine On
RewriteCond %{HTTP_HOST} ^mydomain.com [NC]
RewriteRule ^(.)$ http://www.mydomain.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.
)$ dispatch.fcgi [QSA,L]

ErrorDocument 500 /500.html

#ErrorDocument 500 "