Published on Sep 26 2014 in Java Tomcat

UrlRewriteFilter for Tomcat can do most of the stuff mod_rewrite Apache module does. In a hosting scenario where some URLs are served by Apache and others by Tomcat you need to use correct rewriting method for it to succeed.

Here go 2 most popular use case for URL rewriting: redirect HTTP to HTTPS and redirect www to non-www (or the other way).

For mod_rewrite you can usually use rewrite recipes in .htaccess (or directly in Apache config if you have access to it). For UrlRewriteFilter in Tomcat you should ensure the correct jar is in web application's WEB-INF/lib or in Tomcat's lib directory. In the below example we will apply the filter to default Tomcat web application in webapps/ROOT.

Redirect HTTP to HTTPS in Tomcat (force HTTPS)

Download the UrlRewriteFilter jar (unless you already have it in classpath or your application's WEB-INF/lib):

cd $CATALINA_HOME/lib && wget http://urlrewritefilter.googlecode.com/files/urlrewritefilter-4.0.3.jar

Insert the filter at the top of web.xml of your web application (webapps/ROOT in this case):

cd $CATALINA_HOME/conf
sed -i 's{">{">\
<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>\
{' $CATALINA_HOME/webapps/ROOT/WEB-INF/web.xml

Set rewriting rules in urlrewrite.xml (webapps/ROOT/WEB-INF/urlrewrite.xml in this case):

cat > $CATALINA_HOME/webapps/ROOT/WEB-INF/urlrewrite.xml <<EOF
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 4.0//EN"
 "http://www.tuckey.org/res/dtds/urlrewrite4.0.dtd">
<urlrewrite>
<rule>
 <condition type="scheme" operator="notequal">https</condition>
 <condition name="host" operator="equal">yourdomain.com</condition>
 <from>^/(.*)</from>
 <to type="permanent-redirect" last="true">https://yourdomain.com/$1</to>
</rule>
</urlrewrite>
EOF

Note: In the above commands replace paths and domain.com to match your needs.

For the URL paths served by web server you can put the following in .htaccess:

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]

Redirect non-www to www in Tomcat

The steps are almost identical as in the prevoius example. The difference is in urlrewrite.xml. Build urlrewrite.xml with the following command:

cat > $CATALINA_HOME/webapps/ROOT/WEB-INF/urlrewrite.xml <<EOF
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 4.0//EN"
 "http://www.tuckey.org/res/dtds/urlrewrite4.0.dtd">
<urlrewrite>
<rule>
 <condition name="host" operator="equal">yourdomain.com</condition>
 <from>^/(.*)</from>
 <to type="permanent-redirect" last="true">http://www.yourdomain.com/$1</to>
</rule>
</urlrewrite>
EOF

And its counterpart for .htaccess controlled URLs would be:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^yourdomain\.com
RewriteRule ^(.*)$ http://www.yourdomain.com$1 [R=301,L]

Final notes

When hosting on shared server you should use our Java Control Panel - Mappings to define which URLs/paths should be served by application server and which ones by web server. Remember that for paths mapped to application server you need to use UrlRewriteFilter instead of mod_rewrite rules. .htaccess files will not be read in such cases (as Tomcat cannot see or interpret them). When hosting on VPS you can define mappings directly in webserver config file.

Do not miss our next article comparing authentication in Tomcat and Apache web server.