UrlRewriteFilter Rule to Remove JSP File Extention

2019-06-11 08:57发布

问题:

I would like to use UrlRewriteFilter to remove JSP file extentions in a generic manner to avoid having to specify individual servlet-mappings for 150+ files. Does anyone know what the rule would be for something like that?

From *.jsp to *

ie: /Login.jsp would be translated to /Login

回答1:

So after much experimentation I found out how to do remove the JSP extension from all pages with urlrewrite. The rule below translates any page that has 1 or more characters in the page URI and forwards it to [page name].jsp

Its important to not fire the rule if its not a JSP page off the context root for my site so I exclude files in folders css, img, css, product-img

<?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 match-type="regex">
         <condition type="request-url" operator="notequal">^.*\.jsp$</condition>

        <condition type="request-url" operator="notequal">/css/.*</condition>
        <condition type="request-url" operator="notequal">/img/.*</condition>
        <condition type="request-url" operator="notequal">/js/.*</condition>
        <condition type="request-url" operator="notequal">/product-img/.*</condition>

        <from>/.+(?:(?!jsp).).$</from>
        <to type="forward">%{request-uri}.jsp</to>
    </rule>
</urlrewrite>

The jar file can be found on tuckey's maven site here https://mvnrepository.com/artifact/org.tuckey/urlrewritefilter/4.0.3 (download link is broken on the main page)

This is his main site urlrewrite main site