go
29 lines · 5 steps
How a www-to-apex redirect middleware works in Gin
A Gin middleware that permanently redirects www hostnames to their apex domain while preserving scheme and path.
Explained by
highlit
1package middleware
2
3import (
4 "net/http"
5 "strings"
6
7 "github.com/gin-gonic/gin"
8)
9
10func RedirectWWW() gin.HandlerFunc {
11 return func(c *gin.Context) {
12 host := c.Request.Host
13 if !strings.HasPrefix(host, "www.") {
14 c.Next()
15 return
16 }
17
18 apex := strings.TrimPrefix(host, "www.")
19
20 scheme := "http"
21 if c.Request.TLS != nil || c.GetHeader("X-Forwarded-Proto") == "https" {
22 scheme = "https"
23 }
24
25 target := scheme + "://" + apex + c.Request.URL.RequestURI()
26 c.Redirect(http.StatusMovedPermanently, target)
27 c.Abort()
28 }
29}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Returning a closure that captures nothing lets a middleware factory produce a reusable handler.
- 2Detecting HTTPS behind a proxy means checking X-Forwarded-Proto, not just the raw TLS connection.
- 3A 301 with the original RequestURI keeps the visitor's path and query intact across the redirect.
Related explainers
go
package streaming import ( "bufio"
Streaming NDJSON logs over HTTP in Go
http-streaming
channels
select
Advanced
10 steps
python
import time import uuid from django.utils.deprecation import MiddlewareMixin
Attaching per-request context in Django
middleware
request lifecycle
multi-tenancy
Intermediate
7 steps
javascript
function evaluate(expression) { const tokens = tokenize(expression); let pos = 0;
Building a recursive descent calculator
parsing
recursion
operator-precedence
Intermediate
8 steps
go
package api import ( "crypto/sha256"
ETag conditional requests in Gin
http-caching
etag
conditional-requests
Intermediate
6 steps
go
func (w *Watcher) resetDebounce(d time.Duration) { if !w.timer.Stop() { select { case <-w.timer.C:
Debouncing a stream of events in Go
debounce
timers
channels
Advanced
7 steps
go
package logging import ( "context"
Deduplicating log attributes in Go's slog
decorator-pattern
structured-logging
immutability
Intermediate
8 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/how-a-www-to-apex-redirect-middleware-works-in-gin-explained-go-b2f2/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.