I love a good clean URL, and recently I decided that I wanted to clean up the way I was handling URLs in my web apps. while this is easily achieved using say nginx or apache, or which ever server you are using. I had a unique requirement that I wanted to be able to handle the rewrites locally without having to have to install nginx or something on top of what I was already using. Also I did not want to have a separate server config to maintain. I was after a way I could have the settings checked in with the web application code so it would work on the server and locally.
Locally I am developing using jettydesktop launcher, and I deploy to a server that’s running jetty, while I could have jetty handle it, it would mean loosing the ability to test locally easily by having the settings within the web app.
After some research and looking around I came across UrlRewriteFilter this is a jar file that can be dropped into the lib directory, a small update to the web.xml file to ensure its loaded. Then you have a configuration file where you can set up all your URL rewrites. The details are shown on the UrlRewriteFilter website, and it’s was a relativity painless to get up and running. But I did run into a small issue. After using the base config and it was all working with that correctly, I applied the changes I wanted for it to work for my web app, and I was getting lots of java string overflow errors. Some Googl’ing around lead me no further, so it became process of elimination to get it working.
The default config for the web.xml is
<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>
But for me to remove the error’s I was seeing I had to remove the <dispatcher> lines to give me the following config:
<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>
After restarting jetty all was good. Few more test’s and then I released it to my web server, and all seems to be working.
This jar file is now part of my base web app structure that I use for all new web apps, and I’ll be updating my other apps soon.