On 02/02/2022 15:21, Benny Kannengießer wrote:
Hi,
I wonder how I could achieve that Tomcat sends a 301 (permanent redirect)
instead of 302 (temporary redirect) when a directory is requested without a
trailing slash.
Currently, when Tomcat receives a request like
http://<domain>/some-directory<http://%3cdomain%3e/some-directory>
it redirects to
http://<domain>/some-directory/<http://%3cdomain%3e/some-directory/>
adding the missing trailing slash.
Apparently the redirect is executed either by the mapper or the DefaultServlet, depending
on the "mapperDirectoryRedirectEnabled" property in the context.
Either way, I would like to have a 301 instead of 302 for SEO reasons.
How can I do this?
Already considered subclassing the Default Servlet but the redirect is in a
private method..
Do I have to write a Valve?
Many thanks for pointing me in the right direction!
If you allow the DefaultServlet to do the redirect you should be able to
do this in a filter.
The directory redirect is the only redirect the Default servlet does and
it doesn't explicitly commit the response so you can take some short-cuts.
I think the following will work:
- map the filter to the default servlet
- once the servlet has executed, check the return code and if it is 302
change it to 301.
Mark
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org