Hi Mark, thanks for the good idea!

I just tried it - but it didn't work..
The response from Tomcat is still 302!
I suspected that may be the response has already been sent on the wire, so I 
added some logging calling "response.isCommitted()" in my filter.

Indeed, the logs show "true"! 
I haven't looked at the source code, but now I think the DefaultServlet somehow 
also commits the response.

So what else could I do? Make the response buffer bigger?
Do you see any other possibility?


-----Ursprüngliche Nachricht-----
Von: Mark Thomas <ma...@apache.org> 
Gesendet: Mittwoch, 2. Februar 2022 18:08
An: users@tomcat.apache.org
Betreff: Re: Redirect with 301 for directory requested without trailing slash

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

Reply via email to