Redirect from Context

The Redirect method sends a redirect response to the client of an absolute or relative target URL. It accepts 2 input arguments, a string and an optional integer. The first parameter is the target url to redirect. The second one is the HTTP status code should be sent among redirection response, If the second parameter is missing, then it defaults to 302 (StatusFound). It can be set to 301 (Permant redirect), StatusTemporaryRedirect(307) or 303 (StatusSeeOther) if POST method.

Redirect(urlToRedirect string, statusHeader ...int)

Usage

Redirect from /home to /.

package main

import "github.com/kataras/iris/v12"

func main() {
    app := iris.New()

    app.Get("/", index)
    app.Get("/home", home)

    app.Listen(":8080")
}

func index(ctx iris.Context) {
    ctx.Writef("Hello, %s!", "World")
}

func home(ctx iris.Context) {
	ctx.Redirect("/", iris.StatusPermanentRedirect)
}

Last updated