Search code examples
jspurlservletswebsphere

Removing trailing "/" at the end of a url path


https://mywebsite.com/contextroot/basic.html

The above url is working fine because my project has basic.html under the "deployedResources/webapp"

But when I enter the url as https://mywebsite.com/contextroot/basic.html/ I am getting 403 forbidden.

How do I remove the "/" from the url automatically and get the file basic.html?


Solution

  • The 403 status code is a result of the Application Server trying to serve this URL as a static resource and finding that it ends on a slash and identifying this as a request to a directory (due to the trailing slash). Directory browsing is disabled by default, thus the 403 forbidden status.

    A possible simple solution to this problem is to add a filter to your web application to remove the trailing slash from the URL. See the accepted answer and question update here for an example:

    How to use a servlet filter in Java to change an incoming servlet request url?

    Another option is to use URL rewrite rules in your Web Server (if you are using one).