(An occasional technical note for future self, and others)
Suppose you have a Tomcat based servlet container with webapps directory name "ORIGINAL", and then you have a reason to rename it as "MOVED":
- /ORIGINAL/servlet/path
- /MOVED/servlet/path
You have a few ways to handle this, but simplest turned out to be rewriting the HTTP URL path in front-end of the server. (Assuming you have at least Tomcat 8.5)
The Tomcat servlet container software has a tool for rewriting request URL paths in same style rules as Apache HTTPD does them. This mechanism is called "RewriteValve".
Simplest way to achieve this is to create a ROOT webapp - create following directory and file structure inside your Tomcat webapps. The "ROOT" name in all uppercase is special allowing this mapping to affect full HTTP URL path, instead of only inside a given servlet.
- server/webapps/ROOT/
- server/webapps/ROOT/META-INF/context.xml
- server/webapps/ROOT/WEB-INF/rewrite.config
<?xml version="1.0" encoding="UTF-8"?><!-- This goes to /META-INF/context.xml --><Context reloadable="false"swallowOutput="true"unpackWAR="false"processTlds="false"><Valve className= "org.apache.catalina.valves.rewrite.RewriteValve" /></Context>
#### This runs in ROOT context path
## ( = "/" ) of the URL##RewriteCond %{REQUEST_PATH} ^/ORIGINAL/.*RewriteRule ^/ORIGINAL/(.*)$ /MOVED/$1 [L,NE]
The mechanism has complicated capability for mapping and regular expression rewriting of the URL paths. Those are simpler thing to master once you get the configuration in place.
What you can use this for includes:
- Aliasing web service URL paths (give service without reporting redirect)
- Moving web service URL paths (report a redirect to the caller)
- "Fixing" an URL bug in popular 3rd party client library
- Creating URL compatibility mappings against older version of the service, or different implementations