# Mod\_ruby used to mimic rewrite

**URL:** <https://rubytalk.org/t/mod-ruby-used-to-mimic-rewrite/12965>\
**Category:** ruby-talk\
**Created:** [9 August 2004 11:53 UTC](https://rubytalk.org/t/mod-ruby-used-to-mimic-rewrite/12965 "2004-08-09T11:53:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jm13](https://avatars.discourse-cdn.com/v4/letter/j/5fc32e/32.png) [@jm13](https://rubytalk.org/u/jm13)\
**Post date:** [9 August 2004 11:53 UTC](https://rubytalk.org/t/mod-ruby-used-to-mimic-rewrite/12965/1 "2004-08-09T11:53:50Z")

</div>

The scenario is the need to rewrite a request for [dummy.example.com](http://dummy.example.com) to another directory. The reasoning behind this is a quirk in our current set up so we can map algorithmically map and it would involve too many rewrite rules to handle manually. The problem is that this seems to be working to the point of modifying the filename, but apache returns 403 to the browser and logs

(2)No such file or directory: Can't open directory for index: /var/www/example.com/dummy/

or some other error even though index.rhtml and index.html exist. As far as I can tell it could be either a problem with the Apache::SiteMap class below or the apache config. Also on that matter does anyone know how I can get the module triggered on the host instead of the directory?

I've attempted to look at [http://modrubby.net/](http://modrubby.net/) but it seems to be down so I've been using the google cache. Anyone care to give me a few pointers, here's extracts from the relevant files,

apache config:

\<IfModule mod\_ruby.c\>  
&nbsp;&nbsp;&nbsp;RubyRequire sitemap  
&nbsp;&nbsp;&nbsp;\<Directory /var/www/example.com/\>  
&nbsp;&nbsp;&nbsp;&nbsp;SetHandler ruby-object  
&nbsp;&nbsp;&nbsp;&nbsp;RubyHandler Apache::SiteMap.instance  
&nbsp;&nbsp;&nbsp;&nbsp;Options +Indexes  
&nbsp;&nbsp;&nbsp;&nbsp;DirectoryIndex index.rhtml index.html index.htm index.html.var  
&nbsp;&nbsp;&nbsp;\</Directory\>

&nbsp;&nbsp;&nbsp;# other stuff  
\</IfModule\>

sitemap.rb contains, with debugging code removed,

require "singleton"  
module Apache  
&nbsp;&nbsp;&nbsp;class SiteMap  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;include Singleton  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@@host2dir = {'[dummy.example.com](http://dummy.example.com)' =\> 'dummy/'}

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def handler(r)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r.content\_type = 'text/html'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r.send\_http\_header  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(Apache::OK) if r.header\_only?

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;document\_root = r.server.document\_root  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(document\_root[-1] != "/") && document\_root += "/"

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if @@host2dir.has\_key?(r.headers\_in['Host'])  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;newdocroot = document\_root + @@host2dir[r.headers\_in['Host']]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;filename = r.filename.sub("#{document\_root}", newdocroot)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "sub: #{document\_root} #{newdocroot} #{filename}\<br\>"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r.filename = filename  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return Apache::DECLINED  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end  
end

---

<div class="post-metadata">

**Author:** ![Kirk\_Haines](https://avatars.discourse-cdn.com/v4/letter/k/c6cbf5/32.png) [@Kirk\_Haines](https://rubytalk.org/u/Kirk_Haines)\
**Post date:** [9 August 2004 13:36 UTC](https://rubytalk.org/t/mod-ruby-used-to-mimic-rewrite/12965/2 "2004-08-09T13:36:27Z")

</div>

I just woke up and my head hurts and I'm operating off the top of my head,  
here, to take this for what it's worth (LOL) but I believe what you want to  
do here is to use mod\_ruby to write a translation handler so that your code  
can give Apache a different address to use during the address translation  
phase. The mod\_ruby directive is

RubyTransHandler CLASSNAME

Hope that helps,

Kirk Haines

> **···**
>
> On Mon, 9 Aug 2004 20:53:50 +0900, jm wrote
> 
> > The scenario is the need to rewrite a request for dummy.example.com  
> > to another directory. The reasoning behind this is a quirk in our  
> > current set up so we can map algorithmically map and it would  
> > involve too many rewrite rules to handle manually. The problem is  
> > that this seems to be working to the point of modifying the filename,  
> > but apache returns 403 to the browser and logs

---

<div class="post-metadata">

**Author:** ![jm13](https://avatars.discourse-cdn.com/v4/letter/j/5fc32e/32.png) [@jm13](https://rubytalk.org/u/jm13)\
**Post date:** [10 August 2004 03:03 UTC](https://rubytalk.org/t/mod-ruby-used-to-mimic-rewrite/12965/3 "2004-08-10T03:03:54Z")

</div>

This has got me as far as having one possible solution. It's not exactly what I was after as I would have been able to indicate any arbitrary directory instead of just tweaking the uri. This limits it to being under document root unless anyone can think of a simple way for this arbitrary method would working this uri rewrite will do. Time to run out the door again. Thanks.

J.

> **···**
>
> On 09/08/2004, at 11:36 PM, Kirk Haines wrote:
> 
> > On Mon, 9 Aug 2004 20:53:50 +0900, jm wrote  
> > \> The scenario is the need to rewrite a request for dummy.example.com  
> > \> to another directory. The reasoning behind this is a quirk in our  
> > \> current set up so we can map algorithmically map and it would  
> > \> involve too many rewrite rules to handle manually. The problem is  
> > \> that this seems to be working to the point of modifying the filename,  
> > \> but apache returns 403 to the browser and logs
> > 
> > I just woke up and my head hurts and I'm operating off the top of my head,  
> > here, to take this for what it's worth (LOL) but I believe what you want to  
> > do here is to use mod\_ruby to write a translation handler so that your code  
> > can give Apache a different address to use during the address translation  
> > phase. The mod\_ruby directive is
> > 
> > RubyTransHandler CLASSNAME
> > 
> > Hope that helps,
> > 
> > Kirk Haines

---

<div class="post-metadata">

**Author:** ![Kirk\_Haines](https://avatars.discourse-cdn.com/v4/letter/k/c6cbf5/32.png) [@Kirk\_Haines](https://rubytalk.org/u/Kirk_Haines)\
**Post date:** [10 August 2004 05:10 UTC](https://rubytalk.org/t/mod-ruby-used-to-mimic-rewrite/12965/4 "2004-08-10T05:10:41Z")

</div>

Move to an earlier phase for your handler. If your handler is a  
RubyInitHandler, I believe you should be able to alter the request pretty  
freely as your Ruby code will gain access to it very early in the process of  
handling it.

Good luck,

Kirk Haines

> **···**
>
> On Tue, 10 Aug 2004 12:03:54 +0900, jm wrote
> 
> > This has got me as far as having one possible solution. It's not  
> > exactly what I was after as I would have been able to indicate any  
> > arbitrary directory instead of just tweaking the uri. This limits  
> > it to being under document root unless anyone can think of a simple  
> > way for this arbitrary method would working this uri rewrite will  
> > do. Time to run out the door again. Thanks.
