The current rails documentation shows simple setup for using rails under an apache vhost. This is fine, and is quite simple, but it doesn’t suit my environment. We don’t use vhosts here, primarily because getting any DNS changes is a royal pain. I decided I wanted my rails app to live at http://host/rails/... and this is what I had to do to make that happen. Your mileage may vary. All of the other Rails specific set up not pertaining explicitly to vhosts must also be done for this to work, this procedure just outlines what I did to get Rails going in my environment.
For this example I have installed rails into my home directory:
/home/scott/rails
My example will use:
as the directory, so:
Alias /rails /home/scott/rails/public
At this point you ought to be able to go to:
and see the congratulations screen.
In order for apache to use the rewrite rules in your .htaccess you will have to make sure \FollowSymlinks is On for your directory. In this example:
<Directory /home/scott/rails/public> Options ExecCGI FollowSymlinks AllowOverride All </Directory>
Will get you started.
If you need to debug the rewrite rules, you can add the following (in your httpd(.)conf, these can’t go in .htaccess):
RewriteLog "/tmp/rewritelog" RewriteLogLevel 9
Some minor changes will need to be made in the public/.htaccess file included with rails. You will have to change your targets to match your URL, in my example:
/dispatch.cgi?controller=$1&action=index
Would be changed to:
/rails/dispatch.cgi?controller=$1&action=index
And so on. Change each of these targets to match your set up and then, after you set up the tutorial, you would be able to access rails via something like:
For example. Included are rules that force localhost access through the CGI method for developing convenience, so if you’re coming from 127.0.0.1 you ought to be able to use:
I (AdamWilliams) found that I had to adjust the rules to handle the Alias.
RewriteRule ^([-_a-zA-Z0-9]+)/$ /myalias/dispatch.cgi?controller=$1&action=index [QSA] [L]
Had to be changed to:
RewriteRule ^/([-_a-zA-Z0-9]+)/$ /myalias/dispatch.cgi?controller=$1&action=index [QSA] [L]
Note that if your eyes are as old as mine and you can’t spot the difference between these two versions of the \RewriteRule, it is that in the second version there’s a forward slash (/) immediately after the carat (^) mark.—LyleJohnson
I found that once I set up the installtion with Apache2 without virtual hosts, I was still running into problems.
If I tried:
I would get an error in the logs to the effect of
Exception throw during dispatch: No such file to load -- cgi_controller
C:/ruby/lib/ruby/site_ruby/1.8/rubygems/loadpath_manager.rb:5:in `require__'
C:/ruby/lib/ruby/site_ruby/1.8/rubygems/loadpath_manager.rb:5:in `require'
C:/ruby/lib/ruby/gems/1.8/gems/rails-0.8.5/lib/dispatcher.rb:34:in `dispatch'
C:/dev/blog/public/dispatch.cgi:10
To fix this, I used:
Which prompted another error:
Exception throw during dispatch: directory /tmp does not exist
C:/ruby/lib/ruby/1.8/pstore.rb:25:in `initialize'
C:/ruby/lib/ruby/1.8/cgi/session/pstore.rb:72:in `new'
C:/ruby/lib/ruby/1.8/cgi/session/pstore.rb:72:in `initialize'
C:/ruby/lib/ruby/1.8/cgi/session.rb:264:in `new'
C:/ruby/lib/ruby/1.8/cgi/session.rb:264:in `initialize'
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-0.9.5/lib/action_controller/cgi_process.rb:70:in `new'
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-0.9.5/lib/action_controller/cgi_process.rb:70:in `session'
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-0.9.5/lib/action_controller/base.rb:558:in `assign_shortcuts'
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-0.9.5/lib/action_controller/base.rb:250:in `process'
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-0.9.5/lib/action_controller/base.rb:242:in `process'
C:/ruby/lib/ruby/gems/1.8/gems/rails-0.8.5/lib/dispatcher.rb:35:in `dispatch'
C:/epc/blog/public/dispatch.cgi:10
To fix that, I had to create a “tmp” directry on my C: drive.
-ChrisWilliams
Under the new 0.10.0 and Routes, you have very few things needing change from the default install.
One addtion to public/.htaccess, before any of the \RewriteRule or \RewriteCond lines:
RewriteBase /rails/
And one change to the same file (remove leading ”/” for dispatch.cgi):
from
RewriteRule ^(.*)$ /dispatch.fcgi?$1 [QSA,L]RewriteRule ^(.*)$ dispatch.fcgi?$1 [QSA,L]
You may want to update the \ErrorDocument lines there as well, depending on your site.
config/routes.rb (comments removed):
ActionController::Routing::Routes.draw do |map|
map.connect '/rails/:controller/service.wsdl', :action => 'wsdl'
map.connect '/rails/:controller/:action/:id'
end
-JimHelm
I was having a lot of difficulty with 400: Bad Request\! Errors when dealing with the public directory and the lack of a trailing slash. I am running a rails project out of a user cgi-bin directory aliased to /user-cgi-bin/. The solution I came up with was hacked from a mod_rewrite discussion (see Reverse proxy as well) . The following is my final .htaccess:
# General Apache options
# Added handler for html, since html files were being treated as CGI scripts
AddHandler default-handler .html
AddHandler fastcgi-script .fcgi
AddHandler cgi-script .cgi
# RemoveHandler .html
Options +FollowSymLinks +ExecCGI
# Redirect all requests not available on the filesystem to Rails
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} -d
RewriteCond %{SCRIPT_FILENAME} ^.*[^\/]$
RewriteRule ^(.*)$ $1/ [N]
RewriteBase /user-cgi-bin/test_rails/public/
RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ dispatch.cgi?$1 [QSA,L]
# In case Rails experiences terminal errors
ErrorDocument 500 /user-cgi-bin/test_rails/public/500.html
I had the problem with the trailing slash, too. But Brett’s rewrite rules didn’t work for me. I had to change the first three rules to this:
RewriteCond %{SCRIPT_FILENAME} -d RewriteCond %{REQUEST_URI} ^.*[^\/]$ RewriteRule ^(.*)$ $1/
Otherwise Apache somehow appended a second slash, if there was already a trailing slash in the original URI.
—MatthiasSchuetz?
I installed rails under an alias in my apache installation:
Alias /booking "/var/www/booking/public" <Directory /var/www/booking/public/> Options ExecCGI FollowSymLinks AllowOverride all Allow from all Order allow,deny </Directory>
This made the default RewriteXXX?’s in public/.htaccess go haywire.
The easy solution to this problem was to add a RewriteBase to the .htaccess:
RewriteBase /booking
This seems to make everything work like intended.
—MartinKihlgren
My installation is in the root of the account, meaning the same directory as public_html ”/home/user/”. I did this in public_html:
ln -s ../railsapp/public railsapp
Did this in .htaccess:
RewriteBase /intranett
RewriteRule ^([^.]+)$ $1/ [QSA]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]
RewriteRule ^([^.]+)$ $1.html [QSA]
I removed all the other rewrite rules, so I am not sure which of these rules that are originally included or not. Works like a dream now.