redirect

package
v1.5.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 23, 2026 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package redirect provides HTTP redirect middleware for common URL normalization patterns in Celeris.

Nine constructor functions return a celeris.HandlerFunc and accept an optional Config to override the redirect status code or skip requests:

All constructors default to HTTP 301. Set Config.Code to 308 to preserve the original request method across the redirect. Query strings are always preserved. Register with celeris.Server.Pre so normalization runs before route lookup.

Documentation

Full guides and examples: https://goceleris.dev/docs/middleware-routing-helpers

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func HTTPSNonWWWRedirect

func HTTPSNonWWWRedirect(config ...Config) celeris.HandlerFunc

HTTPSNonWWWRedirect redirects HTTP requests to HTTPS and www to non-www in a single redirect. Requests that are already on HTTPS without www pass through to the next handler.

The default status code is 301 (Moved Permanently). Note that 301 allows browsers to change the request method (e.g., POST becomes GET). Use Config{Code: 308} to preserve the original method.

Example
package main

import (
	"github.com/goceleris/celeris/middleware/redirect"
)

func main() {
	// Redirect to HTTPS + non-www in a single redirect.
	_ = redirect.HTTPSNonWWWRedirect()
}

func HTTPSRedirect

func HTTPSRedirect(config ...Config) celeris.HandlerFunc

HTTPSRedirect redirects HTTP requests to HTTPS. HTTPS requests pass through to the next handler. The redirect URL preserves the original host, path, and query string.

The default status code is 301 (Moved Permanently). Note that 301 allows browsers to change the request method (e.g., POST becomes GET). Use Config{Code: 308} to preserve the original method.

Example
package main

import (
	"github.com/goceleris/celeris/middleware/redirect"
)

func main() {
	// Redirect all HTTP traffic to HTTPS.
	_ = redirect.HTTPSRedirect()
}
Example (Permanent)
package main

import (
	"github.com/goceleris/celeris/middleware/redirect"
)

func main() {
	// Use 308 Permanent Redirect to preserve request method.
	_ = redirect.HTTPSRedirect(redirect.Config{Code: 308})
}
Example (SkipPaths)
package main

import (
	"github.com/goceleris/celeris/middleware/redirect"
)

func main() {
	// Skip HTTPS redirect for health check and readiness probe endpoints.
	_ = redirect.HTTPSRedirect(redirect.Config{
		SkipPaths: []string{"/health", "/healthz", "/ready"},
	})
}

func HTTPSWWWRedirect

func HTTPSWWWRedirect(config ...Config) celeris.HandlerFunc

HTTPSWWWRedirect redirects HTTP requests to HTTPS and non-www to www in a single redirect. Requests that are already on HTTPS with www pass through to the next handler.

The default status code is 301 (Moved Permanently). Note that 301 allows browsers to change the request method (e.g., POST becomes GET). Use Config{Code: 308} to preserve the original method.

Example
package main

import (
	"github.com/goceleris/celeris/middleware/redirect"
)

func main() {
	// Redirect to HTTPS + www in a single redirect.
	_ = redirect.HTTPSWWWRedirect()
}

func NonWWWRedirect

func NonWWWRedirect(config ...Config) celeris.HandlerFunc

NonWWWRedirect redirects www requests to the non-www host. Requests without the www prefix pass through to the next handler. The redirect URL preserves the original scheme, path, and query string.

The default status code is 301 (Moved Permanently). Note that 301 allows browsers to change the request method (e.g., POST becomes GET). Use Config{Code: 308} to preserve the original method.

Example
package main

import (
	"github.com/goceleris/celeris/middleware/redirect"
)

func main() {
	// Redirect www to non-www host.
	_ = redirect.NonWWWRedirect()
}

func RemoveTrailingSlashRedirect

func RemoveTrailingSlashRedirect(config ...Config) celeris.HandlerFunc

RemoveTrailingSlashRedirect strips a trailing slash from the request path. The root path "/" and paths without a trailing slash pass through to the next handler.

The default status code is 301 (Moved Permanently). Note that 301 allows browsers to change the request method (e.g., POST becomes GET). Use Config{Code: 308} to preserve the original method.

Example
package main

import (
	"github.com/goceleris/celeris/middleware/redirect"
)

func main() {
	// Strip the trailing slash from paths.
	_ = redirect.RemoveTrailingSlashRedirect()
}

func RemoveTrailingSlashRewrite

func RemoveTrailingSlashRewrite(config ...Config) celeris.HandlerFunc

RemoveTrailingSlashRewrite strips a trailing slash from the request path in-place. Unlike RemoveTrailingSlashRedirect, this does not send a redirect response -- the request is processed with the modified path.

Example
package main

import (
	"github.com/goceleris/celeris/middleware/redirect"
)

func main() {
	// Strip trailing slash in-place without sending a redirect.
	_ = redirect.RemoveTrailingSlashRewrite()
}

func TrailingSlashRedirect

func TrailingSlashRedirect(config ...Config) celeris.HandlerFunc

TrailingSlashRedirect adds a trailing slash to the request path when missing. The root path "/" and paths already ending with "/" pass through to the next handler.

The default status code is 301 (Moved Permanently). Note that 301 allows browsers to change the request method (e.g., POST becomes GET). Use Config{Code: 308} to preserve the original method.

Example
package main

import (
	"github.com/goceleris/celeris/middleware/redirect"
)

func main() {
	// Add a trailing slash to paths that are missing one.
	_ = redirect.TrailingSlashRedirect()
}

func TrailingSlashRewrite

func TrailingSlashRewrite(config ...Config) celeris.HandlerFunc

TrailingSlashRewrite adds a trailing slash to the request path in-place. Unlike TrailingSlashRedirect, this does not send a redirect response -- the request is processed with the modified path.

Example
package main

import (
	"github.com/goceleris/celeris/middleware/redirect"
)

func main() {
	// Add trailing slash in-place without sending a redirect.
	_ = redirect.TrailingSlashRewrite()
}

func WWWRedirect

func WWWRedirect(config ...Config) celeris.HandlerFunc

WWWRedirect redirects non-www requests to the www subdomain. Requests already on the www subdomain pass through to the next handler. The redirect URL preserves the original scheme, path, and query string.

The default status code is 301 (Moved Permanently). Note that 301 allows browsers to change the request method (e.g., POST becomes GET). Use Config{Code: 308} to preserve the original method.

Example
package main

import (
	"github.com/goceleris/celeris/middleware/redirect"
)

func main() {
	// Redirect non-www to www subdomain.
	_ = redirect.WWWRedirect()
}

Types

type Config

type Config struct {
	// Skip defines a function to skip this middleware for certain requests.
	Skip func(c *celeris.Context) bool

	// SkipPaths lists paths to skip (exact match).
	SkipPaths []string

	// Code is the HTTP redirect status code. Must be 301, 302, 303, 307, or 308.
	// Default: 301 (Moved Permanently).
	Code int
}

Config defines the redirect middleware configuration.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL