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 .rbIf you would like to use .rhtml without deleoping a full Ruby on Rails application...
.htaccess
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"
erb.cgi
#!/usr/local/bin/ruby require 'time' require 'erb' include ERB::Util time = Time.now.httpdate 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 = File.open(path) { |f| ERB.new(f.read) } 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'] = "admin@yourdomain.com" # email message MESSAGE = " From: Erb <admin@yourdomain.com> To: You <you@yourdomain.com> Subject: Error on YourDomain.com ****Script Error**** #{ $! } ****Backtrace**** #{$!.backtrace.join("\n")} ****Environment**** #{ENV.keys.map { |key| key + ' = ' + ENV[key] + "\n"} }" # send the message require "net/smtp" Net::SMTP.start('localhost', 25, 'yourdomain.com', 'admin+yourdomain.com', 'yourPassword', :login) do |smtp| smtp.send_message MESSAGE, 'admin@yourdomain.com', 'you@yourdomain.com' end end
upload the erb.cgi to the ./cgi-bin/ folder of your "application"
chmod permissions of erb.cgi to 755If you want to use a .rhtml page for your 404 errors, add to the .htaccess file:
ErrorDocument 404 /404.rhtml
Add to Favourites Print this Article
Also Read