Tomcat is not adding trailing slash to web app's context
Solution 1
It's an old post, but as of Tomcat 7.0.67, you need to add the following attribute to your context.xml file:
<Context mapperContextRootRedirectEnabled="true">...</Context>
As per the 7.0.67 changelog:
Move the functionality that provides redirects for context roots and directories where a trailing / is added from the Mapper to the DefaultServlet. This enables such requests to be processed by any configured Valves and Filters before the redirect is made. This behaviour is configurable via the mapperContextRootRedirectEnabled and mapperDirectoryRedirectEnabled attributes of the Context which may be used to restore the previous behaviour.
And in the Tomcat context documentation:
mapperContextRootRedirectEnabled: If enabled, requests for a web application context root will be redirected (adding a trailing slash) if necessary by the Mapper rather than the default Servlet. This is more efficient but has the side effect of confirming that the context path exists. If not specified, the default value of false is used.
Solution 2
It seems that your application's web.xml has a mapping to "/*"
. A servlet-mapping to "/*"
causes tomcat to pass the request as-is to the web application (i.e. does not redirect).
To properly redirect, you must change the "/*"
mapping to just "/"
, the latter means the default servlet.
Solution 3
Tomcat adds a trailing slash automatically. Just test it with the example application supplied with Tomcat..
If - due to some special configuration - it does not, I'd write a Filter that examines the query string and redirects as needed by the application. Many times this is needed anyways (doing http->https redirections, etc.)
Badweather
Updated on June 07, 2022Comments
-
Badweather about 2 years
I'd like to have Tomcat automatically add a trailing slash to my app's context if the url is entered without it.
When I test with Jetty, it automatically adds the trailing slash to my app's context, but Tomcat doesn't do this.
I'm uncertain what the context will be named once deployed, as I'm handing the WAR off to someone else, so any resource references in HTML is all relative. Is there any way to have Tomcat automatically redirect to the same context with a trailing slash added?
Currently Using Tomcat 7 with Spring 3.
-
Badweather about 12 yearsYou are right, Tomcat does add these automatically. I need to figure out why this is being disabled in my application then.
-
Istvan Devai about 12 yearsIs there some other server in front of Tomcat? Apache, nginx, etc? Those could mess up redirection..
-
Badweather about 12 yearsActually, a rproxy server in front of it is able to fix the issue by adding trailing slash for me. I believe the issue has to do with Spring MVC accepting my context without a trailing slash as a valid url.
-
Istvan Devai about 12 yearsSpring MVC only gets the request after the proxy and tomcat processed it (including the redirection). My gut feeling is that the proxy messes up the redirect, but you should check out the logs
-
manikanta about 9 years+1 In my case, I've configured Spring Security to intercept
/*
. Is there any config available to Spring Security to add trailing slashes? -
Mark over 8 yearsSneaky Tomcat! I just wasted two whole days trying to figure out what was wrong with my web app! Lesson relearned: if all else fails, RTFM!
-
bekce about 8 yearsThis answer is outdated. See @Pat 's answer.
-
Yosua Lijanto Binar almost 8 yearsThis saves my life, I think the default value of mapperContextRootRedirectEnabled is true, so u don't need add them.
-
Pat almost 8 yearsYes, default has been changed to "true" since 7.0.68
-
Kimball Robinson over 7 yearsThis solved a problem for me. Spring MVC app on Tomcat running behind an F5 load balancer.
-
Guillaume Husta over 7 yearsAccording to the changelog I would have said since 7.0.66 : tomcat.apache.org/tomcat-7.0-doc/…
-
Pat over 7 yearsGuillaume 7.0.66 was never available for download. They jumped from 7.0.65 to 7.0.67. See archive.apache.org/dist/tomcat/tomcat-7