10/08/2008

Url Rewrite Filter: Make Clean URL in Click Framework

Url Rewrite Filter is a servlet filter which provides features similar to the mod_rewrite. We can make clean URL in Click Framework using Url Write Filter.

Url Rewrite Filter has a configuretion file WEB-INF/urlrewrite.xml. This is a very simple example of clean URL with a Click application.

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE urlrewrite
  PUBLIC "-//tuckey.org//DTD UrlRewrite 2.6//EN"
  "http://tuckey.org/res/dtds/urlrewrite2.6.dtd">

<urlrewrite>
  <rule>
    <from>^/contents/([0-9]+).htm$</from>
    <to type="forward">/content.html?contentId=$1</to>
  </rule>
</urlrewrite>
For example, Url Rewrite Filter will forward /contents/001.htm to /content.htm?contentId=001.

If content.htm has possibility to receive any other query parameters, we can define following rules.

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE urlrewrite
  PUBLIC "-//tuckey.org//DTD UrlRewrite 2.6//EN"
  "http://tuckey.org/res/dtds/urlrewrite2.6.dtd">

<urlrewrite>
  <!-- No Query String -->
  <rule>
    <from>^/contents/([0-9]+).htm$</from>
    <to type="forward">/content.html?contentId=$1</to>
  </rule>
  <!-- With Query String -->
  <rule>
    <from>^/contents/([0-9]+).htm?(.+)$</from>
    <to type="forward">/content.html?contentId=$1&$2</to>
  </rule>
</urlrewrite>
Of course, we can use Url Write Filter with other frameworks such as Struts. I think Url Write Filter is a simple solution to make clean URL in the Java web applications.