Powered by Blogger.

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
3. Prerequisites
JDK installed
4. Download Tomcat
5. Tomcat Installation
5.1 Uncompress Apache 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

5.4 Check that tomcat is running
Open the browser in the URL: http://localhost:8080 and you should see the Tomcat Welcome screen.

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.
    Blogger Comment
    Facebook Comment