lundi 2 décembre 2013

Tuckey URL Rewrite


How can we make the web application URL cleaner and prettier instead of showing plenty of URL parameters? Is there a way  for a simple easy solution to do that without configuring the application server like Tomcat? after spending a long time of research and test, i found a tool named  Tuckey URL Rewrite that do exactly what i want.
My needs are the following, i have a java wicket web application with Tomcat 7 as webapp server and want to resolve two problems :

  1. First i would make pretty friend profile link as facebook or Google+, for example applicationName/Firstname.lastname 
  2. Second, i want to implicitly add a dynamic parameter depending on the requested domain name for example :  SuchCustomer.domineName.com should be implicitly transformed to SuchCustomer.domineName.com?CustomertName=SuchCustomer

STEP ONE :

install Tuckey URL Rewrite by adding Maven dependency :
<dependency>
    <groupId>org.tuckey</groupId>
    <artifactId>urlrewritefilter</artifactId>
    <version>4.0.3</version>
</dependency>

STEP TWO :

To WEB-INF/web.xml add :
<filter>
    <filter-name>UrlRewriteFilter</filter-name>
    <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>UrlRewriteFilter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher>
</filter-mapping>

STEP THREE :

Add urlrewrite.xml in WEB-INF (src/main/webapp/WEB-INF/ for Maven users)  , this file will contain all our rules. so to meet my needs, i write  this two rules:
<rule>
<note>Clean Friend profile URL</note>
<from>^/([a-z0-9]+)\.([a-z0-9]{1,10})$</from>
<to>/FriendProfile?friendLoginID=$1</to>
</rule>

<rule>
<name>Add parameter to URL</name>
<condition name="host" operator="equal">SuchCustomer.DomainName.com</condition>
<from>^/$</from>
<to>?CustomerName=SuchCustomer</to>
</rule>    

 For more information about this tool, take a look at the official documentation.

Aucun commentaire:

Enregistrer un commentaire