Trailing slash at the end of url ends up in 404

Hey here!

I’ve a quick question. Why does the play router interpret
https://test/test/ and https://test/test as different urls?

Thanks,
felixoi

1 Like

Hey there,

In the routes documentation the Play! team notes the following:

Dynamic parts are named. The Controller can later retrieve the dynamic parts from the HTTP params map.

By default play considers the trailing URL slash as important. For example, this route:

GET     /clients         Clients.index

will match the /clients URL but not the /clients/ one. You can tell play that you want to match both > URL adding a question mark after the trailing slash. For example:

GET     /clients/?       Clients.index

The URI pattern cannot have any optional part excepted that trailing slash.

Hope this helps.

2 Likes

You link to play 1.x, I’m using 2.x :) sorry if this wasn’t clear

Sorry about that, I’ve been looking at Play 1.x questions all day and got mixed up. Not sure why I made such an assumption.

In short, that’s the default behaviour in the underlying Akka HTTP. Here’s an exert from the relevant documentation:

Note that trailing slash and non-trailing slash URLs are ‘’‘not’’’ the same, although they often serve
the same content. It is recommended to serve only one URL version and make the other redirect to it using [[#redirectToTrailingSlashIfMissing]] or [[#redirectToNoTrailingSlashIfPresent]] directive.

It would appear that you can achieve this in Play through a HttpFilter or HttpRequestHandler; hopefully I’ve got the correct version this time.

We use a route like:

GET    /*path/              controllers.Application.untrail(path: String)

Where controllers.Application is:

  def untrail(path: String): EssentialAction = Action { MovedPermanently("/" + path) }

Why does the play router interpret
https://test/test/ and https://test/test as different urls?

Because they are different urls. Traditionally directories end with a / while files don’t. e.g. you can’t access a file with a ending slash like /assets/myLogo.png/

Decide in your application what kind of resource this url resembles and then either do it with a slash at the end or without one.

1 Like