Configuring URL Rewriter

When filter starts it reads WEB-INF/rewriter-config.xml and all META-INF/rewriter-config.xml from classpaths (and jars). The configuration can look like:

<rewriter-config xmlns:b="http://rewriter.softeu.cz/basic/">
    <b:regex>
                <from>^/news/(.*)$</from>
                <to>/news.jsf?id=$1</to>
    </b:regex>
</rewriter-config>

Part xmlns:b="http://rewriter.softeu.cz/basic/" is important because it enables group of tags for rewriting (you can create either your own). By default the rewriter supports these tags:

 <b:regex type="redirect">
    <from>regular expression</from>
    <to>replaced string with $1, $2, ...</to>
 </b:regex>

 <b:facelets />
 
 <b:fixed type="redirect">
        <from>/index.html</from>
        <to>/index.jsf</from>
 </b:fixed>

Currently it supports these redirects:

  • redirect - uses response.sendRedirect()
  • permanent-redirect - redirects with HttpServletResponse.SC_MOVED_PERMANENTLY
  • temporary-redirect - redirects with HttpServletResponse.SC_MOVED_TEMPORARILY

    facelets tag is an alias for:

        <b:regex type="permanent-redirect">
            <from>^([^\?]*).xhtml(.*)$</from>
            <to>$1.jsf$2</to>
        </b:regex>
    
        <b:regex>
            <from>^([^\\?]*)/(\\?.*)?$</from>
            <to>$1/index.jsf$2</to>
        </b:regex>

    fixed is similar to regexp but you don't need to escape special characters for regexp (eg. . * ).

    If you need to access the original URL (the rewrited one is in HttpServletRequest.getRequestURI) you can read request attribute named cz.softeu.rewriter.originalUrl (please prefer variable RewriterFilter.ORIGINAL_URL_ATTR).