How to redirect Request in Go Fiber?

I was working on adding new domain from to today. The migration is finished but I need one more thing when user visit the old url I want to redirect them into the new url.

Redirecting url from server is just setting up the headers. Here's what look like in good old PHP.

header("HTTP/1.1 301 Moved Permanently");

The same thing happen for golang app. Since it follow the expressjs architecture we just need to add one more middleware put it above all your route definition. Better after you create fiber app instance.

app := fiber.New()
app.Use("/", func(ctx *fiber.Ctx) error {
    if ctx.BaseURL() == "" {
        return ctx.Redirect(
            fmt.Sprintf("", ctx.Path()),
    return ctx.Next()

In local development I still need to use the localhost host so we skip when the base url is not the old url.