<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Codethink &#187; url-rewrite</title>
	<atom:link href="https://codethink.no-ip.org/tags/url-rewrite/feed" rel="self" type="application/rss+xml" />
	<link>https://codethink.no-ip.org</link>
	<description>A blog about coding, life, and other arbitrary topics</description>
	<lastBuildDate>Sun, 15 Mar 2026 21:30:15 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.1.29</generator>
	<item>
		<title>[Java] URL Rewriting on Tomcat (or any other servlet container)</title>
		<link>https://codethink.no-ip.org/archives/654</link>
		<comments>https://codethink.no-ip.org/archives/654#comments</comments>
		<pubDate>Tue, 07 Jun 2011 11:51:31 +0000</pubDate>
		<dc:creator><![CDATA[aroth]]></dc:creator>
				<category><![CDATA[coding]]></category>
		<category><![CDATA[configuration]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[servlet]]></category>
		<category><![CDATA[software]]></category>
		<category><![CDATA[filter]]></category>
		<category><![CDATA[url-rewrite]]></category>

		<guid isPermaLink="false">http://codethink.no-ip.org/wordpress/?p=654</guid>
		<description><![CDATA[Here is a very nice little utility I found recently on an unfortunately very difficult to navigate website. In case it&#8217;s not immediately apparent from that site what I am referring to, I&#8217;m talking about the &#8216;UrlRewriteFilter&#8216; utility featured near &#8230; <a href="https://codethink.no-ip.org/archives/654">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Here is a very nice little utility I found recently on an unfortunately very <a href="http://www.tuckey.org/" target="_blank">difficult to navigate website</a>.  In case it&#8217;s not immediately apparent from that site what I am referring to, I&#8217;m talking about the &#8216;<em>UrlRewriteFilter</em>&#8216; utility featured near the top of the page.  This handy Filter implementation allows you to configure <a href="http://httpd.apache.org/docs/current/mod/mod_rewrite.html" target="_blank">mod_rewrite</a> style URL rewriting rules for your J2EE webapp.  If you are having trouble navigating the official site, you can use this <a href="http://urlrewritefilter.googlecode.com/files/urlrewritefilter-3.2.0.zip">direct link</a> to download &#8216;<em>UrlRewriteFilter</em>&#8216; version 3.2.</p>
<p>Sadly, apart from being difficult to navigate, some of the information on the official &#8216;<em>UrlRewriteFilter</em>&#8216; website pertaining to setup and usage of the filter is also incorrect/out of date.  This is really quite a shame, because &#8216;<em>UrlRewriteFilter</em>&#8216; is an excellent little utility and I&#8217;m quite tired of seeing people needlessly running multi-server configurations (typically <a href="http://httpd.apache.org/" target="_blank">Apache httpd</a> for static content, and something like Tomcat, Resin, Jetty, etc. for dynamic content) out of a desire to use this-or-that particular module that only works with httpd or (even worse) out of the outdated and no-longer-relevant notion that servlet containers cannot efficiently serve static content.  So in an effort to save &#8216;<em>UrlRewriteFilter</em>&#8216; from obscurity and an undeserved death at the hands of poor documentation and a shoddy distribution site, here is a complete set of instructions for getting the filter to work in your webapp.</p>
<p>First, you will need to ensure that the &#8216;<em>UrlRewriteFilter</em>&#8216; JAR file is on your web-application&#8217;s classpath.  How you do this will depend upon your build process/how you are constructing your webapp(s), but long story short placing the JAR file in your webapp under &#8216;/WEB-INF/lib&#8217; will do the trick, and if you&#8217;ve spent any time at all working with webapps you probably already have a preferred way of doing this.  Alternately, you may want to install the JAR file in your servlet container&#8217;s &#8216;/lib&#8217; folder, particularly if you are deploying multiple webapps on your server and you want to have &#8216;<em>UrlRewriteFilter</em>&#8216; available to any/all of them automatically.</p>
<p>In any case, once you have the &#8216;<em>UrlRewriteFilter</em>&#8216; JAR on your webapp&#8217;s classpath, the real setup can begin.  Open your application&#8217;s &#8216;<em>web.xml</em>&#8216; file, and add the following filter configuration to your webapp:</p>
<pre class="brush: xml; title: ; notranslate">&lt;!-- URL rewriting --&gt;
&lt;filter&gt;
    	&lt;filter-name&gt;UrlRewriteFilter&lt;/filter-name&gt;
      	&lt;filter-class&gt;org.tuckey.web.filters.urlrewrite.UrlRewriteFilter&lt;/filter-class&gt;
      	&lt;init-param&gt;
        	&lt;param-name&gt;logLevel&lt;/param-name&gt;
        	&lt;param-value&gt;WARN&lt;/param-value&gt;
        &lt;/init-param&gt;
&lt;/filter&gt;
&lt;filter-mapping&gt;
    	&lt;filter-name&gt;UrlRewriteFilter&lt;/filter-name&gt;
    	&lt;url-pattern&gt;/*&lt;/url-pattern&gt;
&lt;/filter-mapping&gt;</pre>
<p>This instructs the servlet container to route every request that the server receives through the &#8216;<em>UrlRewriteFilter</em>&#8216;.  Note that although it is not discussed on the official site, that &#8216;<em>logLevel</em>&#8216; parameter is absolutely essential.  If you omit it, the filter will fail to initialize properly and yield some very bizarre behavior.  </p>
<p>Anyways, once your &#8216;<em>web.xml</em>&#8216; has been updated, the final step is to add a &#8216;<em>urlrewrite.xml</em>&#8216; file in the same directory as your &#8216;<em>web.xml</em>&#8216; file, and configure it to your liking.  Here is an example &#8216;<em>urlrewrite.xml</em>&#8216; file with a couple basic rewrite rules:</p>
<pre class="brush: xml; title: ; notranslate">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;!DOCTYPE urlrewrite PUBLIC &quot;-//tuckey.org//DTD UrlRewrite 3.2//EN&quot;
        &quot;http://tuckey.org/res/dtds/urlrewrite3.2.dtd&quot;&gt;
&lt;urlrewrite&gt;
    &lt;!-- user-account activation link --&gt;
    &lt;rule&gt;
    	&lt;from&gt;/activate/([a-f0-9]+)?(.*)&lt;/from&gt;
     	&lt;to&gt;/userController?action=activateUser&amp;amp;token=$1&amp;amp;$2&lt;/to&gt;
    &lt;/rule&gt;

    &lt;!-- default rules included with urlrewrite --&gt;
    &lt;rule&gt;
        &lt;note&gt;
            The rule means that requests to /test/status/ will be redirected to /rewrite-status
            the url will be rewritten.
        &lt;/note&gt;
        &lt;from&gt;/test/status/&lt;/from&gt;
        &lt;to type=&quot;redirect&quot;&gt;%{context-path}/rewrite-status&lt;/to&gt;
    &lt;/rule&gt;
    &lt;outbound-rule&gt;
        &lt;note&gt;
            The outbound-rule specifies that when response.encodeURL is called (if you are using JSTL c:url)
            the url /rewrite-status will be rewritten to /test/status/.

            The above rule and this outbound-rule means that end users should never see the
            url /rewrite-status only /test/status/ both in thier location bar and in hyperlinks
            in your pages.
        &lt;/note&gt;
        &lt;from&gt;/rewrite-status&lt;/from&gt;
        &lt;to&gt;/test/status/&lt;/to&gt;
    &lt;/outbound-rule&gt; 
&lt;/urlrewrite&gt;</pre>
<p>This defines two rules.  The first simply rewrites URL&#8217;s of the form &#8216;<em>/activate/######?[params]</em>&#8216; to something like &#8216;<em>/userController?action=activateUser&#038;token=######&#038;[params]</em>&#8216;, and the second is the default example rule that comes with &#8216;<em>UrlRewriteFilter</em>&#8216; and allows you to see a basic diagnostic page by pointing your browser at &#8216;<em>[your server]/test/status</em>&#8216;.  </p>
<p>And there you have it.  Quite simple, really, and now there&#8217;s one less reason to continue running two distinct server instances where one would do just as well.</p>
]]></content:encoded>
			<wfw:commentRss>https://codethink.no-ip.org/archives/654/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
