Category Archives: htaccess

How to run Ruby scripts in a browser without mod_ruby

I’m sure this is a horror in dozens of ways, but eruby sucks and my host doesn’t have mod_ruby. So I got creative.

The .htaccess rewrites requests for any file ending in .rb to this handler.php, which confirms that the requested ruby script exists, then executes it and prints the result.

//PHP script to execute ruby scripts when the host doesn't have a cgi handler for .rb
//Use with this .htaccess:

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)\.rb$ handler.php?rb=$1.rb [NC,QSA]

$file = $_GET['rb'];

if(in_array($file, scandir('.')))
foreach($_REQUEST as $key=>$value) if($key != 'rb') $args .= " $key=".urlencode($value);
echo exec(escapeshellcmd('./'.$file.$args));
echo '404- Page not found';

Here’s an example ruby script using this hack- nothing special to do here at all:


require 'cgi'

print "Hello from Ruby!<br>"
print "All of the input variables:<hr>"

ARGV.each do |arg|
print CGI::unescape(arg.to_s)
print "<br>"

All the GET, POST, and COOKIE variables are at your fingertips in ARGV! (You could get them with Ruby’s CGI module as well.)

So here’s what it looks like to users:

Screenshot of ruby without mod_ruby hack

Screenshot of ruby without mod_ruby hack