Rewritten

Rewritten is a lookup-based rewriting engine that rewrites requested URLs on the fly. The URL manipulations depend on translations found in a redis database.

If a matching translation is found, the result of a request is either a redirection or a modification of path and request parameters. For URLs without translation entries the request is left unmodified.

Rewritten takes larges parts from the Resque codebase (which rocks). The gem is compromised of six parts:

  1. A Ruby library for creating, modifying and querying translations

  2. A Sinatra app for displaying and managing translations

  3. A Rack app for identifying subdomains (Rack::Rewritten::Subdomain)

  4. A Rack app for rewriting and redirecting request (Rack::Rewritten::Url)

  5. A Rack app for substituting URLs in HTML pages with their current translation (Rack::Rewritten::Html)

  6. A Rack app for recording requests (Rack::Rewritten::Record)

New Relic Notice

There seem to be unresolved issues when Rack::Rewritten::Html is used in conjunction with the New Relic gem (being a rack app as well). Unfortunately the only workaround so far is either disabling New Relic or Rack::Rewritten::Html (pointers in the right debugging direction or pull requests are welcome).

Overview

The Rewritten library allows you to create new URL translations and

then query for the current “trade language” of an URL.

Rewritten.add_translation('/apple-computer/newton', '/products/4e4d3c6a1d41c811e8000009')
Rewritten.add_translation('/apple/ipad', '/products/4e4d3c6a1d41c811e8000009')
Rewritten.get_current_translation('/products/4e4d3c6a1d41c811e8000009')         # => "/apple/ipad"

Translations are removed in a similar fashion.

Rewritten.remove_translation('/apple-computer/newton', '/products/4e4d3c6a1d41c811e8000009')

Usage in your Rack stack

To take full advantage of the engine you would be using at least the following stack:

use Rack::Rewritten::Subdomain, "example.com", "lvh.me" # only needed for subdomain support
use Rack::Rewritten::Url
use Rack::Rewritten::Html
run Your::App

This way the URL rewriting and generation is stays decoupled from your app. For a Rails app, for instance, you wouldn’t need to mess with your routes.rb or path helpers when dealing with custom URLs.

The Front End

Rewritten comes with a Sinatra-based front end for dislaying and managing your URL translations (in the familiar Resque layout).

IMAGE

Standalone

Running Rewritten as a gem in standalone mode is easy:

$ rewritten-web

It’s based on Vegas, a thin layer around rackup, and as such configurable as well:

$ rewritten-web -p 8282

Rack::URLMap

To load Rewritten on a subpath alongside other apps you can make use of URLMap:

run Rack::URLMap.new \

"/"       => Your::App.new,
"/resque" => Rewritten::Server.new

Check lib/test.ru for a functional example.