Skip to content

Handling trailing slashes for directories #542

Open
@mversic

Description

@mversic

Feature Request

I'm willing to implement this if approved

Motivation

I want to support only non-trailing slash paths. To do this I wrap my router with NormalizePathLayer::trim_trailing_slash().layer. This however doesn't work well with ServeDir which will always redirect to a trailing slash path for directories (/kita -> /kita/). Therefore, it's not possible not to have trailing slashes on paths that correspond to directories.

Proposal

There are 3 possibilities on how to handle paths without a trailing slash for directories:

  1. redirect to trailing slash
  2. return index.html (200)
  3. respond with 404 (not found)

I think an enum of 3 variants should be offered with current behavior (redirect) as the default. Additionally, I think that the 3rd variant would make the append_index_html_on_directories deprecated as it would have the same effect

Alternatives

A solution where I would rewrite URL in a layer before this service would have to access filesystem to verify that path corresponds to a directory. This would incur unnecessary IO reads so it would be best to handle it directly within ServeDir

I'm eager to hear other proposals on how to design this API

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions