# Redirect 301 and cache

**URL:** <https://www.ruby-forum.com/t/redirect-301-and-cache/181299>\
**Category:** Rails\
**Created:** [January 9, 2010, 1:14am UTC](https://www.ruby-forum.com/t/redirect-301-and-cache/181299 "2010-01-09T01:14:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![johndel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/65b543/32.png) [@johndel](https://www.ruby-forum.com/u/johndel)\
**Post date:** [January 9, 2010, 1:14am UTC](https://www.ruby-forum.com/t/redirect-301-and-cache/181299/1 "2010-01-09T01:14:51Z")

</div>

I want my rails app to redirect from no-WWW to WWW domain (for example  
if I enter [example.com/test](http://example.com/test) to redirect to [www.example.com/test](http://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

---

<div class="post-metadata">

**Author:** ![johndel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/65b543/32.png) [@johndel](https://www.ruby-forum.com/u/johndel)\
**Post date:** [January 9, 2010, 1:22am UTC](https://www.ruby-forum.com/t/redirect-301-and-cache/181299/2 "2010-01-09T01:22:27Z")

</div>

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](http://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

---

<div class="post-metadata">

**Author:** ![johndel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/65b543/32.png) [@johndel](https://www.ruby-forum.com/u/johndel)\
**Post date:** [January 9, 2010, 12:08pm UTC](https://www.ruby-forum.com/t/redirect-301-and-cache/181299/3 "2010-01-09T12:08:28Z")

</div>

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](http://www).“mydomain”.com/$1 [L,R=301]

ErrorDocument 500 /500.html

#ErrorDocument 500 "

Any other suggestions? Thank you

---

<div class="post-metadata">

**Author:** ![johndel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/65b543/32.png) [@johndel](https://www.ruby-forum.com/u/johndel)\
**Post date:** [January 9, 2010, 1:46pm UTC](https://www.ruby-forum.com/t/redirect-301-and-cache/181299/4 "2010-01-09T13:46:47Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![johndel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/65b543/32.png) [@johndel](https://www.ruby-forum.com/u/johndel)\
**Post date:** [January 9, 2010, 1:18pm UTC](https://www.ruby-forum.com/t/redirect-301-and-cache/181299/5 "2010-01-09T13:18:07Z")

</div>

On Fri, Jan 8, 2010 at 4:13 PM, Yiannis [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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](http://www.example.com)  
ServerAlias [example.com](http://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

---

<div class="post-metadata">

**Author:** ![johndel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/65b543/32.png) [@johndel](https://www.ruby-forum.com/u/johndel)\
**Post date:** [January 9, 2010, 5:51pm UTC](https://www.ruby-forum.com/t/redirect-301-and-cache/181299/6 "2010-01-09T17:51:24Z")

</div>

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](http://mydomain.com) [NC]  
RewriteRule ^(._)$ [http://www.mydomain.com/$1](http://www.mydomain.com/$1) [L,R=301]  
RewriteCond %{REQUEST\_FILENAME} !-f  
RewriteRule ^(._)$ dispatch.fcgi [QSA,L]

ErrorDocument 500 /500.html

#ErrorDocument 500 "
