delvedor / find-my-way

A crazy fast HTTP router
MIT License
1.46k stars 132 forks source link

New option: redirect for trailing slashes #123

Closed mahovich closed 2 years ago

mahovich commented 5 years ago

Find-my-way has aignoreTrailingSlash option that allows to access the same page at the addresses "/foo/" and "/foo".

For SEO it would be more useful to add an option that would redirect all requests with a slash at the end of the URL to the same URL without a slash, ignoring the query string (key and value).

Request HTTP code Response
/foo 200 /foo
/foo/ 301 /foo
/foo?a=1 200 /foo?a=1
/foo/?a=1 301 /foo?a=1

For some users of the site (who change the URL in the address bar), it is important to support the processing of the final slash. It is also important not to duplicate the same page for search robots. To solve this problem this new option would be very helpful.

For the Koa framework, there is middleware solving this task koa-no-trailing-slash. But using it before using the find-my-way router creates another problem: when accessing the non-existing page "/404/", a redirect will occur to the "/404" page and only then an error will occur - an extra redirect occurs here.

Request HTTP code Location
/404/ 301 /404
/404 404

If this task will be solved by find-my-way with a new option, then when accessing "/404/", an error page will immediately appear.

Request HTTP code Location
/404/ 404
/404 404

Therefore, would like to see this option in the router, and not to solve it with a separate functionality.

How do you look at adding such an option?

ivan-tymoshenko commented 2 years ago

I don't know if this is a good idea. @mcollina Any thoughts?

mcollina commented 2 years ago

find-my-way is only a router, it does not send responses back. This is a higher level concern (framework): find-my-way responsibility should be to provide enough metadata for a framework to implement this behavior.

ivan-tymoshenko commented 2 years ago

Can we close it, then?