tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From André Warnier (tomcat)>
Subject Re: How to stop trailing slash redirects?
Date Thu, 06 Jun 2019 14:19:45 GMT
On 06.06.2019 13:28, Konstantin Kolinko wrote:
> чт, 6 июн. 2019 г. в 10:32, Rob Nikander <>:
>> Hi,
>> I would like requests to a webapp to have paths that look like this:
>>      http://server/myapp <http://server/myapp>   not:  http://server/myapp/
>> But for some reason, Tomcat keeps redirecting the 1st to the 2nd. I’m aware of
the attribute `mapperContextRootRedirectEnabled` (on the Context), but setting that to “false”
is not helping.  My web.xml looks like:
>>      <servlet-mapping>
>>          <servlet-name>MyServlet</servlet-name>
>>          <url-pattern></url-pattern>
>>          <url-pattern>/other</url-pattern>
>>          …
>> What is going on? How do I stop the redirects?
> 1. Exact version number of Tomcat =? Does it support that attribute?
> 2. I think that you have to make your servlet the default one,
> i.e. to map it to <url-pattern>/</url-pattern> just like it is done
> with DefaultServlet in the default web.xml.
> A filter can be mapped like that as well.
> 3. Beware that session cookies have "/" appended to their Path
> attribute by default.
> There is a Context attribute that configures that, but beware that
> when a cookie path does not have the trailing '/', some browsers will
> send this cookie both to myapp and to myappFoo (with any suffix).
> In general, disabling the redirect is a bad idea and should be avoided.

I cannot point to a specific RFC spec right now, but I am quite sure that this is also a 
requirement/recommendation for HTTP in general : if the URL given is 
"http://myhost/something", and "something" is found by the webserver as being a directory,

the server sends back a redirect to the browser, with the "correct" URL 
(In any case, Apache httpd does that, in its "map to storage" phase).
I believe that doing otherwise may create a lot of confusion as to how to handle "index 
documents" or relative URLs *on the browser side*.

Example :
- suppose your application responding (directly, without redirection) to the URL 
"http://myhost/myapp". And the first page which this application returns to the browser, 
contains a link like :
<img src="myicon.gif" />
(by which you probably mean a file located at the URL "http://myhost/myapp/myicon.gif")
Since the browser now knows that the current page came from "http://myhost/myapp", in 
order to evaluate the (relative) link "myicon.gif", it will
a) use as base the URL of the current page : "http://myhost/myapp"
b) from this URL, strip the last "non-slash-terminated element", thus here "myapp", 
leaving "http://myhost/"
c) to this, add the relative URL "myicon.gif", thus giving "http://myhost/myicon.gif", and

use this URL to retrieve the image from the server.
Which is probably not what you wanted.

This is the expected behaviour of browsers, and the expected behaviour for most other web


In summary, not letting the webserver do what it normally does, is probably going to land

you in some difficult-to-diagnose trouble further down the line.

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message