Url Rewrite Filter

written by aext on July 4, 2008 in Tutorials with no comments

Based on the popular and very useful mod_rewrite for apache, UrlRewriteFilter is a Java Web Filter for any J2EE compliant web application server (such as Resin, Orion or Tomcat), which allows you to rewrite URLs before they get to your code. It is a very powerful tool just like Apache’s mod_rewrite.

URL rewriting is very common with Apache Web Server (see mod_rewrite’s rewriting guide) but has not been possible in most java web application servers. The main things it is used for are:

  • URL Tidyness / URL Abstraction – keep URLs tidy irrespective of the underlying technology or framework (JSP, Servlet, Struts etc).
  • Browser Detection – Allows you to rewrite URLs based on request HTTP headers (such as user-agent or charset).
  • Date based rewriting – Allows you to forward or redirect to other URL’s based on the date/time (good for planned outages).
  • Moved content – enable a graceful move of content or even a change in CMS.
  • Tiny/Friendly URL’s (i.e. blah.com/latest can be redirected to blah.com/download/ver1.2.46.2/setup.exe)
  • A Servlet mapping engine (see Method Invocation)

UrlRewriteFilter uses an xml file, called urlrewrite.xml (it goes into the WEB-INF directory), for configuration. Most parameters can be Perl5 style Regular Expressions or Wildcard Expressions. This makes it very powerful indeed.


Go to orignal site of URLRewriter and download sources code or Binaries, http://tuckey.org/urlrewrite/.

How to use?

You can go to General Example page of Tuckey to see how to use URLRewriter or following guide below.

Create your default web project and:

  1. Download the zip and extract it into your context’s directory ie, so that urlrewrite.xml goes into the WEB-INF directory.
  2. Add the following to your WEB-INF/web.xml (add it near the top above your servlet mappings (if you have any)):
  3. Add your own configuration to the WEB-INF/urlrewrite.xml that was created.
  4. Restart the context.

You can visit http://localhost:8080/rewrite-status (or whatever the address of your local webapp and context) to see output (note: this page is only viewable from localhost).

When installed the filter, only file need to edit is urlrewrite.xml (it goes into the WEB-INF directory). Now, edit your file and see how to make the URLRewriter works. Each url was re-wroten, it’s defined in urlrewrite.xml and beetween <rule> … </rule>

  • Example:
  <from>URL of the page which you want to rewrite</from>
  <to>URL of the new page you cleaned</to>
Work with Servlet

You can forward request from URL to servlet and set action of servlet you want to perform. For default if not use URLRewrite Filter, URL to perform action in servlet is:


In above, the action doExample will be performed inside example servlet. With URLRewite you can do that by:


Following is code to use example, the request /example/doExample will be forwarded to /servlets/exampleServlet and inside the servlet request.getAttribute(“action”) will return doExample.

  <set name="action">doExample</set>
Method Invocation

The standard servlet mapping that is done via web.xml is rather limiting. Only *.xxx or /xxxx/*, no abilty to have any sort of smart matching. Using UrlRewriteFilter any rule when matched can be set to run method(s) on a class.

Invoke a servlet directly

  <run class="yourpackage.ExampleServlet" method="doGet" />

This will invoke doGet(HttpServletRequest request, HttpServletResponse response) when the “from” is matched on a request. (remeber this method needs to be public!). You can use to invoke more than one methods by following example:

  <run class="yourpackage.ExampleServlet" method="method1" />

  <run class="yourpackage.ExampleServlet" method="method2" />
URL Abstraction

Both incoming request and embedded links in JSP’s can be rewritten allowing full URL abstraction.



Any incoming requests for /tidy/page will be transparently forwarded to /old/url/scheme/page.jsp.

If you use JSTL your JSP page would have something like:

<a href="<c:url value="/old/url/scheme/page.jsp"/>">some link</a>

This will be rewritten upon output to:

<a href="/tidy/page">some link</a>

Or if you use standard JSP:

<a href="<%= response.encodeURL("/old/url/scheme/page.jsp") %>">some link</a>

Will generate output like:

<a href="/tidy/page">some link</a>
Other feature

  • Tiny/Freindly url: when type /zebra brower wil send redirect to /big/ugly/url/1,23,56,23132.html
  <to type="redirect">/big/ugly/url/1,23,56,23132.html</to>

  • Default page as another: same as welcome-file in web.xml but is redirect.
<to type="redirect">/opencms/opencms/index.html</to>

  • Disable access to a directory:
    • .htaccess in php with apache server:
      Options -Indexes
    • Use URLRewrite Filter:
        <name>Disable Directory</name>
        <set type="status">403</set>
  • Clean URL:
    • .htaccess in php with apache server:
      RewriteRule ^worlds/([a-z]*)/([a-z]*)$ /world.jsp?country=$1&city=$2 [L]
    • Use URLRewrite Filter:

/world/unitedstates/newyork will be passed on to /world.jsp?country=unitedstates&city=newyork

Other features go to guide page at orginal site for help.

Best Rergad !