Skip to content

Commit dd2a902

Browse files
authored
Merge pull request #4552 from hwupathum/new-filter
Implement redirection for trailing slash
2 parents d6ab2bb + 5299681 commit dd2a902

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright (c) 2026, WSO2 LLC. (http://www.wso2.com).
3+
*
4+
* WSO2 LLC. licenses this file to you under the Apache License,
5+
* Version 2.0 (the "License"); you may not use this file except
6+
* in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing,
12+
* software distributed under the License is distributed on an
13+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
* KIND, either express or implied. See the License for the
15+
* specific language governing permissions and limitations
16+
* under the License.
17+
*/
18+
package org.wso2.carbon.tomcat.ext.valves;
19+
20+
import org.apache.catalina.connector.Request;
21+
import org.apache.catalina.connector.Response;
22+
import org.apache.catalina.valves.ValveBase;
23+
24+
import java.io.IOException;
25+
import javax.servlet.ServletException;
26+
27+
/**
28+
* Valve that normalizes request URIs by redirecting to remove trailing slashes.
29+
* Only redirects paths longer than "/" that end with trailing slashes and have no query string.
30+
*/
31+
public class RequestNormalizationValve extends ValveBase {
32+
33+
@Override
34+
public void invoke(Request request, Response response) throws IOException, ServletException {
35+
36+
String uri = request.getDecodedRequestURI();
37+
38+
if (uri != null && uri.length() > 1 && uri.endsWith("/") && request.getQueryString() == null) {
39+
uri = uri.replaceAll("/+$", "");
40+
response.sendRedirect(uri);
41+
return;
42+
}
43+
44+
// Invoking other valves.
45+
if (getNext() != null) {
46+
getNext().invoke(request, response);
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)