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:
-
A Ruby library for creating, modifying and querying translations
-
A Sinatra app for displaying and managing translations
-
A Rack app for identifying subdomains (Rack::Rewritten::Subdomain)
-
A Rack app for rewriting and redirecting request (Rack::Rewritten::Url)
-
A Rack app for substituting URLs in HTML pages with their current translation (Rack::Rewritten::Html)
-
A Rack app for recording requests (Rack::Rewritten::Record)
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).
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')
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.
Rewritten comes with a Sinatra-based front end for dislaying and managing your URL translations (in the familiar Resque layout).
- IMAGE
-
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
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.