Rewrite url for webapp, similar to htaccess

Webapp doesn’t seem to support htaccess file. Therefore, url rewrite can be done by using UrlRewriteFilter. It can be downloaded from: http://tuckey.org/urlrewrite/

This package is very easy to install:

1. Download the package

2. Put the library in to lib folder under WEB-INF or add as dependencies through maven

3. Put the filter to web.xml in WEB-INF

<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>
</filter-mapping>

4. Copy and Edit urlrewrite.xml to create the rule.

I used this UrlRewriteFilter to take the trailing slashes off the url

For example:

From http://abc.def/ghi/jkl

To http://abc.def/ghi/jkl

What I did was:

1. Tell UrlRewrite to use context path

<urlrewrite use-context=”true”>

2. Add this rule

<rule>
<from>^(.+)/(.+)/$</from>
<to type=”redirect”>$1/$2</to>
</rule>

This may not be a perfect solution but it works 🙂

Link to go back to the root of webapp in jsp

I have been thinking about the code to simply go back to the root of the webapp.

For example: URL/myapp

The solution is really easy:

<a href=”.”>Return To Home Page</a>