Skip to content

UrlHandlerFilter should not strip the trailing slash from root URLs #33444

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ghillert opened this issue Aug 28, 2024 · 5 comments
Closed

UrlHandlerFilter should not strip the trailing slash from root URLs #33444

ghillert opened this issue Aug 28, 2024 · 5 comments
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Milestone

Comments

@ghillert
Copy link

Affects: 6.2.0-M7

Issue #31366 added the new UrlHandlerFilter. That filter should handle root URLs differently by default and not strip the trailing slash from a root URL. It seem reasonable to have a blanket UrlHandlerFilter such as the following in your Spring application:

UrlHandlerFilter.trailingSlashHandler("/**").redirect(HttpStatus.PERMANENT_REDIRECT).build();

For example, a URL such as http://example.com/ is invalid without a trailing slash and browsers (or CURL) do in fact send a trailing slash if you do not specify it for root URLs.

See also: https://webmasters.stackexchange.com/questions/35643/is-trailing-slash-automagically-added-on-click-of-home-page-url-in-browser

@ghillert
Copy link
Author

May also be relevant for #32830

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Aug 28, 2024
@bclozel bclozel added in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Aug 29, 2024
@bclozel bclozel added this to the 6.2.x milestone Aug 29, 2024
@bclozel bclozel self-assigned this Sep 2, 2024
@bclozel bclozel closed this as completed in be5d5fa Sep 2, 2024
@bclozel bclozel modified the milestones: 6.2.x, 6.2.0-RC1 Sep 2, 2024
@bclozel
Copy link
Member

bclozel commented Sep 2, 2024

Thanks Gunnar for this report (and testing our milestones)!

@bdshadow
Copy link
Contributor

bdshadow commented Sep 18, 2024

i think, the problem here is more complex.

We have:

server.servlet.contextPath=/myApp

And use the following UrlHandlerFilter:

        UrlHandlerFilter filter = UrlHandlerFilter
            .trailingSlashHandler("/**").wrapRequest()
            .build();

And in this case, both http://localhost:8080/myApp/items and http://localhost:8080/myApp/items/ work fine, however,
http://localhost:8080/myApp and http://localhost:8080/myApp/ don't and return 404.

Controller for the root looks like this:

@Controller
@RequestMapping("/")
public class MyAppUiController {
    @GetMapping(value = {"/", "greeting"}, produces = MediaType.APPLICATION_JSON_VALUE)
    public ModelAndView greeting() {
        ModelAndView mav = new ModelAndView("greeting");
        return mav;
    }
}

So when you have server.servlet.contextPath specified, it doesn't work

@bclozel
Copy link
Member

bclozel commented Sep 18, 2024

@bdshadow I don't think this is related to this particular issue. Can you create a new one with a minimal application that reproduces the problem?

@bdshadow
Copy link
Contributor

@bclozel thank you for your reply.
Here is the issue: #33565
and a minimal app: https://github.com/bdshadow/SpringTrailingSlashTestProject

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

4 participants