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:
- HTTPSRedirect — redirects HTTP to HTTPS
- WWWRedirect — redirects non-www to www
- NonWWWRedirect — redirects www to non-www
- TrailingSlashRedirect — adds a trailing slash when missing
- RemoveTrailingSlashRedirect — strips a trailing slash
- HTTPSWWWRedirect — redirects to HTTPS + www in one hop
- HTTPSNonWWWRedirect — redirects to HTTPS + non-www in one hop
- TrailingSlashRewrite — adds a trailing slash in-place (no redirect)
- RemoveTrailingSlashRewrite — strips a trailing slash in-place (no redirect)
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 ¶
- func HTTPSNonWWWRedirect(config ...Config) celeris.HandlerFunc
- func HTTPSRedirect(config ...Config) celeris.HandlerFunc
- func HTTPSWWWRedirect(config ...Config) celeris.HandlerFunc
- func NonWWWRedirect(config ...Config) celeris.HandlerFunc
- func RemoveTrailingSlashRedirect(config ...Config) celeris.HandlerFunc
- func RemoveTrailingSlashRewrite(config ...Config) celeris.HandlerFunc
- func TrailingSlashRedirect(config ...Config) celeris.HandlerFunc
- func TrailingSlashRewrite(config ...Config) celeris.HandlerFunc
- func WWWRedirect(config ...Config) celeris.HandlerFunc
- type Config
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()
}
Output:
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()
}
Output:
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})
}
Output:
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"},
})
}
Output:
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()
}
Output:
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()
}
Output:
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()
}
Output:
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()
}
Output:
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()
}
Output:
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()
}
Output:
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()
}
Output:
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.