Search code examples
tomcaturl-rewritingtomcat8tomcat-valve

Tomcat URL Redirect Issue


Tomcat : 8.5.30 Java 8 OS : macOS Mojave 10.14.6

As a sample project, I'm trying to redirect any url : /{dynamicValue}/entity/{entityID} to /entity/{dynamicValue}/entity/{entityID}. Here the 'entity' is my context and will be referencing entity.war inside the webapps directory.

Context File (Path : apache-tomcat-8.5.30/conf/context.xml):

<Context docBase="ROOT" path="/" reloadable="true" crossContext="true">
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>
<Valve className="org.apache.catalina.valves.rewrite.RewriteValve"/>
</Context>

RewriteConfig File : (Path : apache-tomcat-8.5.30/webapps/ROOT/WEB_INF/rewrite.config.xml)

RewriteRule ^/(.*)/(.*)/(.*)$   $2/$1/$2/$3 [L,NE]

Exposed endpoint : /{dynamicValue}/entity/{entityID}

Internal Endpoint : /entity/{dynamicValue}/entity/{entityID}

Any call directly made to 'Internal Endpoint' works fine. But, API calls to 'Exposed Endpoint' are not being redirected to the 'Internal Endpoint'. (returning 404)

Any suggestions ?

Reference : Re-writing tomcat 8 urls


Solution

  • Issue in rewrite.config file name.

    Furthermore, Tomcat does not support dynamic/regex based contexts and that is because of well defined principles. If you have such a use case, I'd strongly advise figuring out the exact need for this and if you could improve the modelling.