XRX/URL Rewriting
Motivation
editYou want the URLs/URIs in your system to reflect the logical structure of a service, not the collection structure of your database.
Method 1: Use the Jetty Rewrite Handler
editUse the Jetty Rewrite Handler that can be installed with the Jetty Server. The RewriteHandler uses an XML configuration file to store the rewrite patterns of a URI. This file can contain regular expressions that allow a single rule to be used rewrite a large number of URIs.
Method 2: Use an Atom interface
editThe eXist native XML database has an Atom Publishing Protocol interface. You can use this to create new URL interfaces to existing XQueries.
Example
editSuppose you use XQuery to execute a report of all terms that have changed in the last N days for project P. Using an eXist XQuery the URL might be
http://example.com/exist/rest/db/apps/glossary/views/terms-changed.xq?days=N&project=P/
The URL rewrite might be:
http://example.com/terms/new/days=N/project=P/
This makes the URL become a more stable application program interface and allows you to move the XQuery to another location in the eXist database and change parameter names without impacting your consumers, their bookmarks, or other systems that access this data through a REST interface.
Example Using Apache Mod Rewrite
editThe most popular way to implement URL rewriting is to use the Apache web server as a front end to your web application. This allows you to convert "dynamic" URLs into ones that appear to be "static", where there are no question marks or ampersands in the URL string and parameters are passed as though they were folders rather than key-value pairs.
Here is the code that would be added to the Apache configuration file:
Options +FollowSymLinks
RewriteEngine on
RewriteRule terms/new/days=(.*)/project=(.*)/ db/apps/glossary/views/terms-changed.xq?days=$1&project=$2
See also
editAn XForms application to manage Jetty rewrite URLS.
References
edit- Wikipedia Article on Rewrite Engine
- Jetty Rewrite Handler
- Tool to help you learn how to write Apache rewrite rules
- Mastering Regular Expressions, Jeffrey E.F. Friedl, Nutshell Handbook Series, O'Reilly & Associates, Inc. 1997,ISBN 1-56592-257-3
- Excellent example of how Drupal handles URL rewrites with their PATH module