WikiGardening in progress. We’re combining/reorganizing several related howto’s at the moment, apologies for the dust. As a first stage, we are gathering all the related questions and their answers in one place and this is the place.
All right. A great use of using rails with rewritten URLs is Apache with mod_userdir. Allowing every user to run rails may be something you want to pursue. So here’s an example of using rails with rewritten urls.
Setting up rails in Apache with mod_userdir is really simple.
First, you’ll want to set up your applications in your home. I will use the standard public_html path.
cd ~sean/public_html/ rails railsfun cd railsfun/publicEdit the .htaccess file and add RewriteBase.
This is an example:
/home/sean/public_html/railsfun/public/.htaccess
# General Apache options
AddHandler fastcgi-script .fcgi
AddHandler cgi-script .cgi
#Options +FollowSymLinks +ExecCGI
# If you don't want Rails to look in certain directories,
# use the following rewrite rules so that Apache won't rewrite certain requests
#
# Example:
# RewriteCond %{REQUEST_URI} ^/notrails.*
# RewriteRule .* - [L]
# Redirect all requests not available on the filesystem to Rails
# By default the cgi dispatcher is used which is very slow
#
# For better performance replace the dispatcher with the fastcgi one
#
# Example:
# RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]
RewriteEngine On
RewriteBase /~sean/railsfun/public/
RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]
# In case Rails experiences terminal errors
# Instead of displaying this message you can supply a file here which will be rendered instead
#
# Example:
# ErrorDocument 500 /500.html
ErrorDocument 500 "<h2>Application error</h2>Rails application failed to start properly"
This will set base to the public folder in the rails application. Note: Don’t set it explicitly to dispatch.cgi/dispatch.fcgi unless you don’t want to serve up static pages (e.g. index.html)
The only catch to this is the url will end up looking like thishttp://hostname/~sean/railsfun/public/ rather than just ~sean/railsfun or ~sean, but this is solved through either a simple redirection page or a .htaccess file in the root of the rails application with an alias or a Redirect directive. Both can be dangerous; a safer alternative is the mod_rewrite path.
An easy, straight-forward way to redirect to the public folder is to create a .htaccess file in the root of the rails application with a very simple rule.
This is an example:
/home/sean/public_html/railsfun/.htaccess
RewriteEngine On RewriteRule (.*)http://hostname/~sean/railsfun/public/$1 [L,R]
This is tested on Apache 2.x.
There is a trailing slash problem. It is required that URL end with a trailing slash, but if we look into mod_rewrite a little more, we can deduce that we can rewrite the URL to include the slash and then redirect to the public root.
This makes sure that any requests what-so-ever that go to ~sean/railsfun are directed to ~sean/railsfun/public.
Here’s how!
/home/sean/public_html/railsfun/.htaccess
RewriteEngine on RewriteBase /~sean/railsfun/ RewriteCond %{REQUEST_FILENAME} -d RewriteRule ^(.+[^/]+)$ $1/ [R] RewriteRule .*/railsfun/(.*)http://hostname/~sean/railsfun/public/$1 [L,R] #Else Slash Detected RewriteRule (.*)http://hostname/~sean/railsfun/public/$1 [L,R]
This takes care of our little trailing slash problem. All requests will make their way to public directory. So http://hostname/~sean/railsfun/index.html will fall through to http://hostname/~sean/railsfun/public/index.html.
And there we have a fully rewritten URL without worries about redirecting to public.
RewriteRule ^.*myapp$ http://%{HTTP_HOST}/myapp/ [R=301,L]