# Apache mod\_rewrite External Rewriting Program

**URL:** <https://rubytalk.org/t/apache-mod-rewrite-external-rewriting-program/41828>\
**Category:** ruby-talk\
**Created:** [1 November 2007 04:49 UTC](https://rubytalk.org/t/apache-mod-rewrite-external-rewriting-program/41828 "2007-11-01T04:49:48Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Patrick\_Joyce](https://avatars.discourse-cdn.com/v4/letter/p/6a8cbe/32.png) [@Patrick\_Joyce](https://rubytalk.org/u/Patrick_Joyce)\
**Post date:** [1 November 2007 04:49 UTC](https://rubytalk.org/t/apache-mod-rewrite-external-rewriting-program/41828/1 "2007-11-01T04:49:48Z")

</div>

I needed to do a bit of complicated URL rewriting for a project I'm  
working on. Apache's mod\_rewrite's built in capabilities wouldn't do, so  
I decided to make an External Rewriting Program  
([http://httpd.apache.org/docs/2.2/mod/mod\_rewrite.html#rewritemap](http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewritemap)) to do  
it.

All the examples I could find were in Perl, but I wanted to take a stab  
at it in Ruby. What I have works most of the time, but occasionally  
fails with what appears to be a concurrency issue.

Here is a simplified version of what I have:

> **···**
>
> ---------------------  
> #!/opt/csw/bin/ruby
> 
> # Turn off buffering  
> STDIN.sync = true  
> STDOUT.sync = true
> 
> # Enter loop  
> while (STDIN)  
> &nbsp;&nbsp;host = gets  
> &nbsp;&nbsp;if host =~ /^(.+)\.somedomain\.com$/  
> &nbsp;&nbsp;&nbsp;&nbsp;puts $1  
> &nbsp;&nbsp;else  
> &nbsp;&nbsp;&nbsp;&nbsp;puts 'NULL'  
> &nbsp;&nbsp;end  
> end  
> ---------------------
> 
> And here are the pertinent rewrite\_log entries ("key" is what is passed  
> to my program via STDIN, "val" is what my program returns to STDOUT):
> 
> ---------------------  
> [[mysubdomain.somedomain.com/sid#81b3d48](http://mysubdomain.somedomain.com/sid#81b3d48)][rid#837ef38/initial] (5) map  
> lookup OK: map=lookup\_subdomain [key=mysubdomain.somedomain.com](http://key=mysubdomain.somedomain.com) -\>  
> val=msbomain  
> [[mysubdomain.somedomain.com/sid#81b3d48](http://mysubdomain.somedomain.com/sid#81b3d48)][rid#837ef38/initial] (5) map  
> lookup OK: map=lookup\_subdomain [key=mysubdomain.somedomain.com](http://key=mysubdomain.somedomain.com) -\>  
> val=yudmysubdomain  
> ---------------------
> 
> So... "[mysubdomain.somedomain.com](http://mysubdomain.somedomain.com)" is passed via STDIN to my program  
> twice. The first time returns "msbomain" and the second returns  
> "yudmysubdomain"
> 
> Isn't this program single threaded? Any ideas as to how my output is  
> being interleaved?
> 
> I'm running ruby 1.8.5 (2006-12-25 patchlevel 12) [i386-solaris2.8] on a  
> Joyent Accelerator.
> 
> Thanks is advance for your help.
> 
> - Patrick Joyce  
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Patrick\_Joyce](https://avatars.discourse-cdn.com/v4/letter/p/6a8cbe/32.png) [@Patrick\_Joyce](https://rubytalk.org/u/Patrick_Joyce)\
**Post date:** [1 November 2007 19:07 UTC](https://rubytalk.org/t/apache-mod-rewrite-external-rewriting-program/41828/2 "2007-11-01T19:07:24Z")

</div>

OK, so I'm now actually pretty sure this isn't a problem with my Ruby  
code, but a problem with either Solaris or Apache.

I went ahead and took the Perl example from the Apache docs, modified it  
slightly to match my example, and still got the same problem:  
intermittently interleaved results.

This makes me pretty confident that the problem is outside of Ruby  
(unless of course, and I'm not ruling this out, I somehow made the same  
mistake in both Ruby and Perl).

Since it doesn't seem to be a Ruby issue I don't expect any help here,  
but if anyone has ANY idea what could be going on I would be forever in  
your debt.

- Patrick Joyce

The Perl is below.

> **···**
>
> -----------------  
> #!/usr/bin/perl
> 
> #disable buffered I/O which would lead  
> #to deadloops for the Apache serve  
> $| = 1;
> 
> #read URLs one per line from stdin and  
> #generate substitution URL on stdout  
> while (\<\>) {  
> &nbsp;&nbsp;&nbsp;&nbsp;/^(.+)\.somedomain\.com$/;  
> &nbsp;&nbsp;&nbsp;&nbsp;print $1, "\n";  
> }  
> -----------------
> 
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
