Home » Enterprise Java » Tomcat » Apache Tomcat Rewrite Rules Example

About Jesus Boadas

Jesus Boadas
I'm a self taught programmer, I began programming back in 1991 using an IBM A10 mainframe with Pascal an Assembler IBM 360/70 emulator and Turbo C on a X86 PC, since that I work for the banking industry with emerging technologies like Fox Pro, Visual Fox Pro, Visual Basic, Visual C++, Borland C++, lately I moved out to the Airline industry, leading designing and programming in-house web applications with Flex, Actionscript, PHP, Python and Rails and in the last 7 years I focused all my work in Java, working on Linux servers using GlassFish, TomCat, Apache and MySql.

Apache Tomcat Rewrite Rules Example

The rewrite valve implements URL rewrite functionality in a way that is very similar to mod_rewrite from Apache HTTP Server. uses a rule-based rewriting engine, based on a PCRE regular-expression parser, to rewrite requested URLs.
 
 
 
 
 
 
 
 

 
URL rewriting is a technique called URL rewriting that can turn unsightly URLs into nice ones. It enables you to fill out your URLs with friendly, readable keywords without affecting the underlying structure of your pages.

1. The tools

  • Java JDK
  • Apache Tomcat

2. Introduction

In this example we are going to use the rewrite valve to show how it works.

3. Prerequisites

  • JDK installed

4. Download Tomcat

Go to the page https://tomcat.apache.org/download-80.cgi and download the tomcat server as a zip compressed file for windows.

1 Download Tomcat for Windows

1 Download Tomcat for Windows

5. Tomcat Installation

5.1 Uncompress Apache Tomcat

Choose an installation directory and uncompress the Tomcat server in its own directory.

4 Uncompress Tomcat

4 Uncompress Tomcat

5.2 Install the Tomcat service

Open the Windows terminal and go to the Tomcat Installation bin directory.

Tomcat installation directory

C:\Java\Apache Tomcat 8.0.15\bin>

Install the service with the following command:

Install Tomcat service

C:\Java\Apache Tomcat 8.0.15\bin>service install

You should get an output similar to this:

install Tomcat output

Installing the service 'Tomcat8' ...
Using CATALINA_HOME:    "C:\Java\Apache Tomcat 8.0.15"
Using CATALINA_BASE:    "C:\Java\Apache Tomcat 8.0.15"
Using JAVA_HOME:        "C:\Java\jdk1.8.0_40"
Using JRE_HOME:         "C:\Java\jre1.8.0_40"
Using JVM:              "C:\Java\jre1.8.0_40\bin\client\jvm.dll"
The service 'Tomcat8' has been installed.

5.3 Start the Tomcat service

Start the service with the following command:

Start tomcat output

C:\Java\Apache Tomcat 8.0.15\bin>sc start Tomcat8

You should get an output similar to the following:

console

SERVICE_NAME: Tomcat8
        TYPE               : 10  WIN32_OWN_PROCESS
        STATUS             : 2  START_PENDING
                                (NOT_STOPPABLE, NOT_PAUSABLE, IGNORES_SHUTDOWN)
        WIN32_OUTPUT_CODE  : 0  (0x0)
        SERVICE_OUTPUT_CODE: 0  (0x0)
        CHECK-POINT        : 0x0
        START-INDICATOR    : 0x7d0
        PID                : 5552
        MARKS              :

5.4 Check that tomcat is running

Open the browser in the URL: http://localhost:8080 and you should see the Tomcat Welcome screen.

5 Tomcat Welcome

5 Tomcat Welcome

6. Configure rewrite valve

You need to include the rewrite valve class org.apache.catalina.valves.rewrite.RewriteValve in your application’s context. This can be in the global context.xml or in the context block of a host in the server.xml. Create a rewrite.config file containing your rewrites into your application WEB-INF folder.

6.1 Configure valve in Tomcat context.xml

Using the global context.xml will affect all virtual host.

RewriteValve Context

<?xml version='1.0' encoding='utf-8'?>
<!-- The contents of this file will be loaded for each web application -->
<Context>
    <Valve className="org.apache.catalina.valves.rewrite.RewriteValve" />
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
    <WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>
</Context>

6.2 Configure valve on an individual host

If we want to set up the valve rewrite rule only on an individual host, we need to edit the server.xml and add the RewriteValve class to the virtual host.

RewriteValve Virtual Host

<Host name="JavaCodeGeeks.com" appBase="webapps" unpackWARs="true" autoDeploy="true">
  <Context path="" docBase="C:/devel/java/www">
    <Valve className="org.apache.catalina.valves.rewrite.RewriteValve" />
  </Context>
</Host>

7. Rewrite rules

As we say earlier rewrite valve is similar to Apache HTTPD mod_rewrite so we can use similar regular expression rules to make the rewrite work.

If you are using a global rewrite to effect all virtual hosts, then we can drop your rewrite.config into the WEB-INF. For individual virtual hosts, We will need to locate the WEB-INF of our application.

We are going to create a rewrite rule to make our url’s pretty

so for example:

Original URL

http://www.javacodegeeks.com/w/index.jsp?title=RewriteRule

Are rewritten to

Rewritten URL

http://www.javacodegeeks.com/app/RewriteRule

Our rule is going to be:

 

RewriteRule   ^app/(.+)$   w/index.jsp?title=$1   [L]

Remember we can use this rule on our server context or on an individual virtual host.

8. Conclusion

Tomcat Rewrite valve comes in handy when we need to make multiple rewriting tasks like pretty urls, URL redirection, etc. This is useful to have in Tomcat, because we don’t need to install another server layer to do these simple tasks. Rewrite valve is easy to configure and uses standard and well knows rewriting rules.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

4 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Jetpanja
Jetpanja
1 year ago

If I configure rewriteValve globally or either at Host level, could you please specify exact location where to place rewrite.config? (In the 7th point it isn’t clearly mentioned!!)

Isaac Jefferson
Isaac Jefferson
1 year ago
Reply to  Jetpanja

I Think it will be in either /conf/localhost/rewrite.config or inside WEB-INF of your application.

JP Jason
JP Jason
1 year ago

“i think” – tomcat documentation really sucks! i need global rewrite rules, not application or context specific and i really don’t find where to put this rewrite.config. Logs does not show any errors either… rewrite just does not work .

harish
1 year ago

does it work for tomcat7?