# REST + Caching + Apache 2.2 Rewrite problem and solution?

**URL:** <https://www.ruby-forum.com/t/rest-caching-apache-2-2-rewrite-problem-and-solution/67112>\
**Category:** Rails\
**Created:** [August 31, 2006, 6:36pm UTC](https://www.ruby-forum.com/t/rest-caching-apache-2-2-rewrite-problem-and-solution/67112 "2006-08-31T18:36:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nauhaie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/3ab097/32.png) [@nauhaie](https://www.ruby-forum.com/u/nauhaie)\
**Post date:** [August 31, 2006, 6:36pm UTC](https://www.ruby-forum.com/t/rest-caching-apache-2-2-rewrite-problem-and-solution/67112/1 "2006-08-31T18:36:06Z")

</div>

Hi,

A lot of us are now using Apache 2.2 + mod\_rewrite to forward incoming  
and uncached requests to a Mongrel Cluster.

That was fine until REST arrived.

With the current recommended configuration, Apache works as follows:

a. There is a static file avaliable in the file system =\> send it  
(regardless to the request method)  
b. There is none, forward request to Mongrel Cluster

However, with RESTful Rails, we have urls such as /articles/1 which  
should be cached only when we GET it, not when we POST or PUT it. What  
if we want to update an article and Apache sends us the static HTML file  
instead of forwarding our request to Rails?

Here is the solution I suggest:

Replace in apache configuration file:

# Redirect all non-static requests to cluster

RewriteCond %{DOCUMENT\_ROOT}/%{REQUEST\_FILENAME} !-f  
RewriteRule ^/(.\*)$ balancer://MyCluster%{REQUEST\_URI} [P,QSA,L]

By

# Redirect all non-static requests to cluster

RewriteCond %{DOCUMENT\_ROOT}/%{REQUEST\_FILENAME} !-f [OR]  
RewriteCond %{REQUEST\_METHOD} !^GET\*  
RewriteRule ^/(.\*)$ balancer://MyCluster%{REQUEST\_URI} [P,QSA,L]

What do you thing about that?

---

<div class="post-metadata">

**Author:** ![nauhaie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/3ab097/32.png) [@nauhaie](https://www.ruby-forum.com/u/nauhaie)\
**Post date:** [August 31, 2006, 11:15pm UTC](https://www.ruby-forum.com/t/rest-caching-apache-2-2-rewrite-problem-and-solution/67112/2 "2006-08-31T23:15:13Z")

</div>

> RewriteCond %{REQUEST\_METHOD} !^GET\*  
> RewriteRule ^/(.\*)$ balancer://MyCluster%{REQUEST\_URI} [P,QSA,L]
> 
> What do you thing about that?

Does it work? Seems like it should. Mongrel has something similar  
built in actually (not that it’ll help in this case).

–  
Rick O.  
[http://weblog.techno-weenie.net](http://weblog.techno-weenie.net)  
[http://mephistoblog.com](http://mephistoblog.com)

---

<div class="post-metadata">

**Author:** ![nauhaie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/3ab097/32.png) [@nauhaie](https://www.ruby-forum.com/u/nauhaie)\
**Post date:** [September 1, 2006, 10:13am UTC](https://www.ruby-forum.com/t/rest-caching-apache-2-2-rewrite-problem-and-solution/67112/3 "2006-09-01T10:13:01Z")

</div>

Yes it should work. But I am looking for people who know more than I do  
about Apache and REST to confirm that this is or is not a good idea…

Someone?

---

<div class="post-metadata">

**Author:** ![nauhaie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/3ab097/32.png) [@nauhaie](https://www.ruby-forum.com/u/nauhaie)\
**Post date:** [March 25, 2008, 5:02pm UTC](https://www.ruby-forum.com/t/rest-caching-apache-2-2-rewrite-problem-and-solution/67112/4 "2008-03-25T17:02:21Z")

</div>

I spent loooong time to locate the problem, and then your solution  
saved me from wasting more. Thanks a lot.

On Sep 1 2006, 12:36 am, Nauhaie N. \<rails-mailing-l…@andreas-
