-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
馃悰 directory need auto redirect #705
Comments
Hi @suntong, |
Thanks for your reply @thomasvvugt, so I took a look and found that 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, 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 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 |
Ah, yeah, that make perfect sense. I.e.,
|
Took a further look at the issue to determine where exactly the problem came from, as fiber is relying on the 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 |
Seems to have come from Lines 273 to 285 in 29e8f12
What is the logic behind such |
See #1490 |
Although it can solve this problem, but it is not the best solution |
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
tohttp://site/directory/
automatically (note the extra trailing/
), so they would not exhibit such differences. I think so should fiber too.The text was updated successfully, but these errors were encountered: