How to Redirect to a Page or URL in Golang HTTP Server

Robin
Updated on December 11, 2023

You can handle HTTP redirects using the net/http package in a Golang server. It has the http.Redirect() method to redirect from one page to another or an external URL.

The syntax:

          http.Redirect(ResponseWriter, Request, URL, StatusCode)
        

The http.Redirect() method accepts 4 arguments in this order: http.ResponseWriter, *http.Request, URL, and HTTP status code.

You should provide a status code from the 3xx range. Because these status codes are used for HTTP redirection.

If you don't set HTTP headers like Content-Type in your route handler function, this method will set the Content-Type header to "text/html; charset=utf-8" by default.

Redirect to Another Page in Golang Server

You have an old page with outdated content, but you want to redirect your users from this page to the new page with the updated content.

Let's see how you can redirect from one page to another page within the same website using Golang:

          package main

import (
	"fmt"
	"net/http"
)

func oldPageHandler(w http.ResponseWriter, r *http.Request) {
	http.Redirect(w, r, "/new-page", http.StatusPermanentRedirect)
}

func newPageHandler(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintln(w, "New page")
}

func main() {
	http.HandleFunc("/old-page", oldPageHandler)
	http.HandleFunc("/new-page", newPageHandler)

	fmt.Println("Server started on port 8000")
	err := http.ListenAndServe(":8000", nil)
	
	if err != nil {
		fmt.Println(err)
	}
}

        

Here I have the /old-page route where I am using the http.Redirect() method to redirect to the /new-page route.

Here I have defined the /new-page as the redirect URL and 308 status code to show it's a permanent redirection.

          http.Redirect(w, r, "/new-page", 308)
        

You can pass the status code directly as a number or use constants from the http package.

Now if you visit the /old-page page, Golang will redirect you to the /new-page page with the permanent redirect status code (308).


Redirect To a Page Using The Handle Method

In this example, I have defined the /old-page route using http.HandleFunc() method and oldPageHandler() function.

But if you don't want to create a route handler function for the redirection, you can define your route using the http.Handle() method.

          package main

import (
	"fmt"
	"net/http"
)

func newPageHandler(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintln(w, "New page")
}

func main() {
	http.Handle("/old-page", http.RedirectHandler("/new-page", http.StatusPermanentRedirect))
	http.HandleFunc("/new-page", newPageHandler)

	fmt.Println("Server started on port 8000")
	err := http.ListenAndServe(":8000", nil)
	
	if err != nil {
		fmt.Println(err)
	}
}

        

Here I have defined my /old-page route using http.Handle() method.

For redirections, instead of using the http.Redirect() method, you need to use the http.RedirectHandler() method.

This method accepts 2 arguments: Redirect URL and Status code.

As the http.Handle() method takes a Golang HTTP handler as its second argument, the http.Redirecthandler() method returns a handler.


Redirect to an External URL From Golang Server

You can also use the http.Redirect() method to redirect from a Golang route to an external URL. You just have to pass the full URL to this method as its third argument.

          package main

import (
	"fmt"
	"net/http"
)

func redirectHandler(w http.ResponseWriter, r *http.Request) {
	http.Redirect(w, r, "https://www.google.com", http.StatusSeeOther)
}

func main() {
	http.HandleFunc("/redirect", redirectHandler)

	fmt.Println("Server started on port 8000")
	err := http.ListenAndServe(":8000", nil)
	
	if err != nil {
		fmt.Println(err)
	}
}

        

I have a /redirect route that will redirect users to the"https://www.google.com" URL with the 303 status code.

You can also use the http.Handle() method to create the /redirect route and redirect to an external URL using the http.RedirectHandler() method.

          package main

import (
	"fmt"
	"net/http"
)

func main() {
	http.Handle("/redirect", http.RedirectHandler("https://www.google.com", http.StatusSeeOther))

	fmt.Println("Server started on port 8000")
	err := http.ListenAndServe(":8000", nil)
	
	if err != nil {
		fmt.Println(err)
	}
}

        

Related Posts