Filed under:

How do I use .RHTML (Ruby Included HTML) Files ?

The .rhtml is an extention for Ruby included in HTML

The default extention for ruby is .rb

If you would like to use .rhtml without deleoping a full Ruby on Rails application...


RewriteEngine Off Options +FollowSymLinks +ExecCGI AddHandler rubypage .rhtml Action rubypage /cgi-bin/erb.cgi DirectoryIndex index.rhtml index.html index.htm 

upload the .htaccess to the root folder of your "application"


#!/usr/local/bin/ruby require 'time' require 'erb' include ERB::Util  time =  HEADERS = <<-EOF Date: #{ time } Server: #{ ENV['SERVER_SOFTWARE'] } Last-Modified: #{ time } Content-Type: text/html  EOF  begin  path = nil if (ENV['PATH_TRANSLATED']) path = ENV['PATH_TRANSLATED'] else file_path = ENV['REDIRECT_URL'].include?(File.basename(__FILE__)) ? ENV['SCRIPT_URL'] : ENV['REDIRECT_URL'] path = File.expand_path(ENV['DOCUMENT_ROOT'] + '/' + file_path) raise "Attempt to access invalid path: #{path}" unless path.index(ENV['DOCUMENT_ROOT']) == 0 end erb = { |f| } print HEADERS + erb.result(binding)  rescue Exception  print "Content-Type: text/html\n\n"  # what the customer sees print "<h1>Page Not Found</h1>"  # modify message variables ENV['SERVER_ADMIN'] = ""  # email message MESSAGE = " From: Erb <> To: You <> Subject: Error on  ****Script Error**** #{ $! }  ****Backtrace**** #{$!.backtrace.join("\n")}  ****Environment**** #{ { |key| key + ' = ' + ENV[key] + "\n"} }"  # send the message require "net/smtp" Net::SMTP.start('localhost', 25, '', '', 'yourPassword', :login) do |smtp| smtp.send_message MESSAGE, '', '' end  end 

upload the erb.cgi to the ./cgi-bin/ folder of your "application"
chmod permissions of erb.cgi to 755

If you want to use a .rhtml page for your 404 errors, add to the .htaccess file:

ErrorDocument 404 /404.rhtml 

Was this answer helpful?

Add to Favourites Add to Favourites    Print this Article Print this Article

Also Read