Search code examples
javaurlurislash

Java web app: prevent a slash from being added to the path?


If I run a webapp under the uri /myapp then as soon as the app is accessed via http://example.com/myapp, the URL changes to http://example.com/myapp/. Is there any way to prevent this?


Solution

  • When you have such a behaviour your web (or application) server returns a

    301 Moved Permanently
    

    when the URL without slash is requested.

    You can see a similar example when getting http://www.google.es/services

    HTTP/1.1 301 Moved Permanently
    Location: http://www.google.es/services/
    Content-Type: text/html; charset=UTF-8
    X-Content-Type-Options: nosniff
    Date: Wed, 11 May 2011 15:24:06 GMT
    Expires: Fri, 10 Jun 2011 15:24:06 GMT
    Cache-Control: public, max-age=2592000
    Server: sffe
    Content-Length: 227
    X-XSS-Protection: 1; mode=block
    

    After this first HTTP get to http://www.google.es/services (without slash), the browser makes a second HTTP get to http://www.google.es/services/ (with slash). You can trace the HTTP requests with Network tab in Firebug, for example.

    You can check your web/application server configuration, and maybe you can change this behaviour.