Ruby on Rails
HowToUseRailsWithRewrittenURLs (Version #36)

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.

Source pages (from which material is still being collected)

All right. A great use of using rails with rewriten 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/public

Edit the .htaccess file and add RewriteBase.

This is an example:

/home/sean/public_html/railsfun/public/.htaccess

  1. General Apache options
    AddHandler fastcgi-script .fcgi
    AddHandler cgi-script .cgi
    #Options +FollowSymLinks +ExecCGI
  1. If you don’t want Rails to look in certain directories,
  2. use the following rewrite rules so that Apache won’t rewrite certain requests
    #
  3. Example:
  4. RewriteCond %{REQUEST_URI} ^/notrails.*
  5. RewriteRule .* – [L]
  1. Redirect all requests not available on the filesystem to Rails
  2. By default the cgi dispatcher is used which is very slow
    #
  3. For better performance replace the dispatcher with the fastcgi one
    #
  4. Example:
  5. 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]
  1. In case Rails experiences terminal errors
  2. Instead of displaying this message you can supply a file here which will be rendered instead

#

  1. Example:
  2. ErrorDocument 500 /500.html

ErrorDocument 500 "

Application error

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 this
http://hostname/~sean/railsfun/public/ rather than just {color:darkblue} ~sean/railsfun or {color:darkblue} ~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.

—-
Unfortunately, the above did not resolve the trailing slash problem for me. I’m using a soft link from ‘public_html’ to the ‘public’ directory of my app (more info on my blog if you’re interested). Anyway, with the above solution, I kept getting ‘/home/public_html/….’ in my redirected URL when I put in my app name without the slash. eg, ‘http://myhost.com/myapp’. The solution I’m using now, after a lot of head banging, is adding this rule to the top of my rewrite rules in .htaccess:

RewriteRule ^.*myapp$ http://%{HTTP_HOST}/myapp/ [R=301,L]

It seems to be working well so far.

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.

Source pages (from which material is still being collected)

All right. A great use of using rails with rewriten 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/public

Edit the .htaccess file and add RewriteBase.

This is an example:

/home/sean/public_html/railsfun/public/.htaccess

  1. General Apache options
    AddHandler fastcgi-script .fcgi
    AddHandler cgi-script .cgi
    #Options +FollowSymLinks +ExecCGI
  1. If you don’t want Rails to look in certain directories,
  2. use the following rewrite rules so that Apache won’t rewrite certain requests
    #
  3. Example:
  4. RewriteCond %{REQUEST_URI} ^/notrails.*
  5. RewriteRule .* – [L]
  1. Redirect all requests not available on the filesystem to Rails
  2. By default the cgi dispatcher is used which is very slow
    #
  3. For better performance replace the dispatcher with the fastcgi one
    #
  4. Example:
  5. 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]
  1. In case Rails experiences terminal errors
  2. Instead of displaying this message you can supply a file here which will be rendered instead

#

  1. Example:
  2. ErrorDocument 500 /500.html

ErrorDocument 500 "

Application error

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 this
http://hostname/~sean/railsfun/public/ rather than just {color:darkblue} ~sean/railsfun or {color:darkblue} ~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.

—-
Unfortunately, the above did not resolve the trailing slash problem for me. I’m using a soft link from ‘public_html’ to the ‘public’ directory of my app (more info on my blog if you’re interested). Anyway, with the above solution, I kept getting ‘/home/public_html/….’ in my redirected URL when I put in my app name without the slash. eg, ‘http://myhost.com/myapp’. The solution I’m using now, after a lot of head banging, is adding this rule to the top of my rewrite rules in .htaccess:

RewriteRule ^.*myapp$ http://%{HTTP_HOST}/myapp/ [R=301,L]

It seems to be working well so far.