UrlRewriteFilter

This tool seems to behave somewhat capriciously. When attempting to do regular expressions you'll see the <to> URL added multiple times, or have stack errors.

My case was to send all requests a redirect to foobar and of course let requests to that page through. You'd think you'd do a nice inverse match, something like   !^/foobar$. You can do a lookahead inverse like  ^(?!/foobar) but these rules are unpredictable at best.

When you want to do an inverse match, the secret is to use a <condition> and limit the handle to request objects.

In your apps WEB-INF/web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
     version="2.5">

<filter>
    <filter-name>UrlRewriteFilter</filter-name>
    <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>

<filter-mapping>
    <filter-name>UrlRewriteFilter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
</filter-mapping>

</web-app>

</filter>


In your apps WEB-INF/urlrewrite.xml

<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 4.0//EN"
        "http://www.tuckey.org/res/dtds/urlrewrite4.0.dtd">

<urlrewrite>

    <rule>
        <note>
            A request for foobar will be allowed thru. Note the use of the - to allow this and the last
            so no more rules are processed
        </note>
            <from>^/foobar$</from>
        <to last="true">-</to>
    </rule>


    <rule>
        <note>
            A request for anything other than foobar will be redirected to foobar
        </note>
        <condition type="path-translated" operator="notequal">/redirect.html</condition> 
        <to type="redirect" last="true">/context/foobar</to>
    </rule>


</urlrewrite>





Notes:

You can add these to your web.xml for debugging.

   <init-param>
            <param-name>confReloadCheckInterval</param-name>
            <param-value>0</param-value>
    </init-param>


    <init-param>
        <param-name>logLevel</param-name>
        <param-value>DEBUG</param-value>
    </init-param>


</filter>
Comments