Tomcat
URL rewriting instructions for Tomcat

Using rewrite Valve

Tomcat 8 can be configured using RewriteValve. See http://tonyjunkes.com/blog/a-brief-look-at-the-rewrite-valve-in-tomcat-8/ for examples.

Instructions for UrlRewriteFilter

UrlRewriteFilter (commonly referred to as Tuckey) is a Java web filter for compliant web application servers such as Tomcat, Jetty, Resin and JBoss. Unfortunately UrlRewriteFilter depends on XML with its extremely strict syntax.
  • Append the servlet-mapping markup to the end of the <filter mapping> element in your WEB-INF/web.xml
  • Add the pretty urls rule markup to the <urlrewrite> element to your WEB-INF/urlrewrite.xml configuration.
  • Restart the web application server.
Servlet-Mapping markup
servlet-mapping
1
<servlet-mapping>
2
<servlet-name>CFMLServlet</servlet-name>
3
<url-pattern>/rewrite.cfm/*</url-pattern>
4
</servlet-mapping>
Copied!
Example markup with UrlRewriteFilter and Wheels pretty URLs for WEB-INF/web.xml.
web.xml
1
<filter>
2
<filter-name>UrlRewriteFilter</filter-name>
3
<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
4
</filter>
5
<filter-mapping>
6
<filter-name>UrlRewriteFilter</filter-name>
7
<url-pattern>/*</url-pattern>
8
<dispatcher>REQUEST</dispatcher>
9
<dispatcher>FORWARD</dispatcher>
10
</filter-mapping>
11
<servlet-mapping>
12
<servlet-name>CFMLServlet</servlet-name>
13
<url-pattern>/rewrite.cfm/*</url-pattern>
14
</servlet-mapping>
Copied!
Pretty URLs Rule markup
pretty urls rule
1
<rule enabled="true">
2
<name>CFWheels pretty URLs</name>
3
<condition type="request-uri" operator="notequal">^/(flex2gateway|jrunscripts|cfide|cf_scripts|cfformgateway|cffileservlet|lucee|files|images|javascripts|miscellaneous|stylesheets|wheels/public/assets|robots.txt|favicon.ico|sitemap.xml|rewrite.cfm)</condition>
4
<from>^/(.*)$</from>
5
<to type="passthrough">/rewrite.cfm/$1</to>
6
</rule>
Copied!
A complete barebones WEB-INF/urlrewrite.xml configuration example with pretty URLs.
urlrewrite.xml
1
<?xml version="1.0" encoding="utf-8"?>
2
<!DOCTYPE urlrewrite
3
PUBLIC "-//tuckey.org//DTD UrlRewrite 4.0//EN"
4
"http://www.tuckey.org/res/dtds/urlrewrite4.0.dtd">
5
6
<urlrewrite>
7
<rule enabled="true">
8
<name>CFWheels pretty URLs</name>
9
<condition type="request-uri" operator="notequal">^/(flex2gateway|jrunscripts|cfide|cf_scripts|cfformgateway|cffileservlet|lucee|files|images|javascripts|miscellaneous|stylesheets|wheels/public/assets|robots.txt|favicon.ico|sitemap.xml|rewrite.cfm)</condition>
10
<from>^/(.*)$</from>
11
<to type="passthrough">/rewrite.cfm/$1</to>
12
</rule>
13
</urlrewrite>
Copied!
Last modified 1mo ago
Export as PDF
Copy link
Edit on GitHub