Aaron Greenlee.com | A Personal Journey.
You should follow me here.
This is the RSS subscription you have been looking for.
 
Wrecking Ball Media
A great team that is clearing the way for digital marketing.

Have you thought about joining the Central Florida Web Developers User Group?

URL Rewriting for Tomcat

POSTED Wednesday, January 26, 2011  |   | DOWNLOAD CODE
Keywords: Tomcat, URL Rewrite

This month it seems as if Amazon has sent a shockwave through the various developer communities I frequent with their announcement of the Amazon Beanstalk. Amazon selected Apache's Tomcat as the servlet container for their ground breaking product and a lot of people have started to ask about Tomcat. A few months ago I read a book on Tomcat and I recommend it to anyone considering deploying with Tomcat. However, the book does not cover URL Rewriting for search engine safe (SES) links–also called pretty urls.

My blog is running Tomcat behind Apache 2.2 For this site I used Apache's mod_rewrite and control the rules with my .htaccess file. But, in the Beanstalk you just have Tomcat–which is fast, secure and full featured and capable of serving massive volume reliably. In fact, the only good argument I have ever read to keep Apache in front of Tomcat was that Apache could continue to serve error messages if Tomcat goes down. There really is no need for Apache as a front end on an environment with redundancy such as the Beanstalk where Web severs live behind a load balancer. Some may argue that you don't want Tomcat serving CSS and media assets–but, in this era we have cheap access to CDNs for those things.

So, if you too are deploying to the Amazon Beanstalk you may want to rewrite your URLs. Fortunately, thanks to the shared efforts of the tuckey.org folks rewriting urls for Tomcat is easy. Just download the Tuckey URL Rewrite Filter and place the contents of the zip in your /WEB-INF directory. Configuring the filter is unique to your needs, but, I've shared a simple example below to help you get started.

web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	version="2.5">

	<display-name>Application Name</display-name>
	<description>
	 Your application should have a description. After all, not
	 everyone can ready your mind! In fact, I have a feeling
	 in about six weeks you may forget this stuff.
	</description>

	<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>
</web-app>
urlrewrite.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 3.2//EN" "http://tuckey.org/res/dtds/urlrewrite3.2.dtd">
 <!--
 
    Configuration file for UrlRewriteFilter
    http://tuckey.org/urlrewrite/
 
-->
<urlrewrite>
	<rule>
		<note>
			Allow TomCat to have SES links.
		</note>
		<!--
			If our URI ends with one of these extentions we will not perform
			a URL rewrite.
		--->
		<condition type="request-uri" operator="notequal">\.(bmp|gif|jpe?g|png|css|js|txt|pdf|doc|xls|xml|cfc|ico|php|asp)$</condition>
		<!--
			If any URI begins with one of these directories, we will not
			perform a rewrite. Helpful for running your unit tests or
			"blocking" rewrite for some directories.
		-->  
		<from>^/(?!cdn|src|railo-context|test|mxunit|remote)(.*)$</from>
		<to>/index.cfm/$1</to>
	</rule>
</urlrewrite>
blog comments powered by Disqus
You can send me email or work with
   me for digital marketing, web design
      and application development.

         I own proprietary web application development
         company and work with a leading digital marketing firm.
© 2009 - 2012 Aaron Greenlee. Powered by my own code on the ColdBox Framework.

This site is best viewed on Chrome, FireFox and Safari. Subscribe to my RSS feed.