Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7016989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:51:24+00:00 2026-05-27T22:51:24+00:00

Spring MVC (3.0) considers URLs with and without trailing slashes as the same URL.

  • 0

Spring MVC (3.0) considers URLs with and without trailing slashes as the same URL.

For example:

http://www.example.org/data/something = http://www.example.org/data/something/

I need to redirect the URL with trailing slashes

http://www.example.org/data/something/

to the URL without it:

http://www.example.org/data/something

I need to do this internally the application (so not rewrite rules via Apache, etc).

A way to do it is:

@ResponseStatus(value=HttpStatus.MOVED_PERMANENTLY)
@RequestMapping(value = "/data/something/")
public String dataSomethingRedirect(...) {
    return "redirect:/data/something";
}

but this has generally 2 problems:

  1. too many controllers
  2. problem with parameters: like wrong encoding

Question

Is there a way to intercept all the URLs and in case they have a trailing slash, redirect them to the relative one without slash?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T22:51:25+00:00Added an answer on May 27, 2026 at 10:51 pm

    You could list all the rewrite rules you need in your web configuration

    If there aren’t many of those, you can configure redirect views like this

    @Configuration
    public class WebConfig extends WebMvcConfigurerAdapter {
      @Override
      public void addViewControllers(ViewControllerRegistry registry) {
        registry.addRedirectViewController("/my/path/", "/my/path")
          .setKeepQueryParams(true)
          .setStatusCode(HttpStatus.PERMANENT_REDIRECT); 
    }
    

    Or you could create a custom HandlerInterceptor

    But interceptors occur before requests are mapped to a specific Controller.action and you’ve got no way of knowing Controllers and actions in that context.

    All you’ve got is HTTPServlet API and request+response; so you can:

    response.sendRedirect("http://example.org/whitout-trailing-slash");
    

    The answer you don’t want to read

    This behavior (URL with trailing slash = URL without it) is perfectly “valid” when considering HTTP. At least this is the default behavior with Spring, that you can disable with useTrailingSlashMatch (see javadoc).

    So using rewrite/redirect rules on the front-end server could a solution; but again, I don’t know your constraints (maybe you could elaborate on this and we could figure out other solutions?).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Imagine that I have a Spring MVC controller something like this: @Controller @RequestMapping(/base-url) public
With Spring MVC, you can specify that a particular URL will handled by a
Spring MVC + Jackson converting Java object to/from Json data. I want to validate
Does Spring MVC support extension-less URLs like asp.net MVC does? I am just getting
I have a Spring-MVC project which needs to have 2 views for example: views/xxx/...
In Spring MVC a Dispatcher servlet is used as a front controller to handle
In Spring MVC when placing an object in the view model like so: public
I am using Spring MVC to build my web application, and I have a
I'm building a Spring MVC web application with Tiles/JSP as the view technology. Previously
I'm migrating a Spring MVC controller to use the newer style annotations, and want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.