Tomcat is not adding trailing slash to web app's context

后端 未结 5 1386
自闭症患者
自闭症患者 2020-12-09 10:10

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

相关标签:
5条回答
  • 2020-12-09 10:16

    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.

    0 讨论(0)
  • 2020-12-09 10:24

    Have you tried playing with URL Rewrite on Tomcat?
    This might help: http://code.google.com/p/urlrewritefilter/

    If that does not help, take a look at this: URL rewrite in tomcat web.xml

    0 讨论(0)
  • 2020-12-09 10:26

    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.

    0 讨论(0)
  • 2020-12-09 10:31

    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.)

    0 讨论(0)
  • 2020-12-09 10:37

    Pat's excellent answer helped me dig up a few more details on this. It seems this is related to some quirks in some versions of Tomcat (Tomcat 7 at 7.0.67+, and Tomcat 8 between 8.29 and 8.37) having to do with session cookies and URL redirection.

    The bottom line seems to be that if the java server creates path-specific session cookies with a slash at the end (like "/app_name/"), then the server must also do an automatic initial redirect (/app_name --> /app_name/) ... otherwise, the session cookie will not get sent with the request, and it will never look to the server like you have a valid session. The may cause a redirect loop from the app to the authentication.

    There are configurations in Tomcat that control both behaviors, but as far as I can tell, they were essentially out-of-sync in these versions, such that one might get the cookie with the trailing slash, without getting the redirect. There are several related issues/changes in the Tomcat changelog: https://tomcat.apache.org/tomcat-8.0-doc/changelog.html

    As Pat has already noted, this is resolved by adding this attribute to your app's Context element:

    <Context mapperContextRootRedirectEnabled="true">
    
    0 讨论(0)
提交回复
热议问题