Fiber version
v1.11.1 & v1.13.3
Code snippet
Take a look at
https://github.com/suntong/fiber_demo/blob/master/app/static-gz.go
Issue description
The results of visiting
are wrong, while visiting the following are right:
Normal web servers would redirect http://site/directory to http://site/directory/ automatically (note the extra trailing /), so they would not exhibit such differences. I think so should fiber too.
Hi @suntong,
Fiber has a setting called StrictRouting to change it's behavior to treat /foo and /foo/ as the same.
Redirecting /foo to /foo/ would require you to write a middleware to check for the trailing /, and redirect the client if it's not present.
To my knowledge, there are no such guidelines to specifically use/not use a trailing / for a URL.
Thanks for your reply @thomasvvugt, so I took a look and found that StrictRouting would actually _remove_ the trailing slashes, which would actually make things worse and not working any more.
Maybe there is no obvious guidelines, but if you take a look at every web server, the above described behaviors are what they actually doing. So maybe it is such a common sense that there is no need for a written guidelines for it.
OK. I'll write a middleware on my own, and submit a PR against this issue.
Thanks
Hi @suntong,
By default, StrictRouting is disabled. When StrictRouting is enabled, it will indeed redirect the client if it requests a route with any trailing slashes (/).
So we can take advantage of Fiber's default behavior by using a simple middleware which does the job of redirecting the client if there are no trailing slashes present in the original URL. I also took note of the fact that clients can also request files with file extensions.
Take a look at my example below, and let me know if that helps you out!
package main
import (
"log"
"regexp"
"strings"
"github.com/gofiber/fiber"
"github.com/gofiber/utils"
)
func main() {
app := fiber.New(&fiber.Settings{
// By default -> StrictRouting: false,
})
// Custom middleware to redirect routes with a trailing `/`
app.Use(func(c *fiber.Ctx) {
originalUrl := utils.ImmutableString(c.OriginalURL())
// Check if the client is requesting a file extension
extMatch, _ := regexp.MatchString("\\.[a-zA-Z0-9]+$", originalUrl)
if !strings.HasSuffix(originalUrl, "/") && !extMatch {
c.Redirect(originalUrl + "/")
}
c.Next()
})
// Route definitions
app.Get("/foo/", func(c *fiber.Ctx) {
c.SendString("I am Foo!")
})
log.Fatal(app.Listen(8080))
}
Thanks for asking! 馃殌
Why it is closed?
Hi @suntong,
Like I pointed out in my previous comment, there are no such guidelines to specifically use/not use a trailing / for a URL.
I have also provided a working code example which adds a trailing slash, mimicking the behavior you stated.
Let me know if there are any additional comments regarding the issue or the given code example.
Fine.
Giving the fact that all normal web servers would redirect directories automatically with the extra trailing, I'm still thinking this should be a builtin feature, not even an added middleware, but
It's your code so it is your call.
It's something we worked on a while back, please see https://github.com/gofiber/fiber/blob/master/app.go#L183. But we never finished the implementation because it impacted the router performance too much.
It's not that we do not want to implement this, but I just couldn't find the time yet to make this feature work without losing to much performance.
PR's are welcome, in the meantime, I have to focus on the v1.14.0 release notes and maybe we can take another look at the RedirectFixedPath feature for v1.14.1 馃憤
Ah, yeah, that make perfect sense. I.e.,
Giving the fact that all normal web servers would redirect directories automatically with the extra trailing, I'm still thinking this should be a builtin feature, not even an added middleware...
Took a further look at the issue to determine where exactly the problem came from, as fiber is relying on the fasthttp to provide the file server service. So I have added a fasthttp-fileserver check --
https://github.com/suntong/fiber_demo/commit/8ab9321a1fc26f4915ebf097b6339ba1dc965e7e
cd fiber_demo/fasthttp-fileserver
go run fileserver.go -dir ../web
The check points out that fasthttp-fileserver clearly has such feature builtin -- it would redirect directories automatically with the extra trailing -- http://localhost:8080/sample0 would become http://localhost:8080/sample0/ automatically.
So the problem is somewhere within fiber, not the fasthttp.
So the problem is somewhere within fiber, not the fasthttp.
Seems to have come from
https://github.com/gofiber/fiber/blob/29e8f12a13966ed67161eae2fc0a856f4603bdfa/router.go#L273-L285
What is the logic behind such PathRewrite?
Would it be a better idea just to leave Path Rewrite to fasthttp to handle that?