Skip to content

Rewrite

Rewrite middleware rewrites the URL path based on the provided rules. It is helpful for backward compatibility or for creating cleaner and more descriptive links.

All core middleware lives in the middleware package:

import "github.com/labstack/echo/v5/middleware"
e.Pre(middleware.Rewrite(map[string]string{
"/old": "/new",
"/api/*": "/$1",
"/js/*": "/public/javascripts/$1",
"/users/*/orders/*": "/user/$1/order/$2",
}))

The values captured in asterisks can be retrieved by index, e.g. $1, $2, and so on. Each asterisk is non-greedy (translated to a capture group (.*?)); when using multiple asterisks, a trailing * matches the rest of the path.

e := echo.New()
e.Pre(middleware.RewriteWithConfig(middleware.RewriteConfig{}))

RewriteConfig · github.com/labstack/echo/v5@5196b9b

Fields from package source
FieldTypeDescriptionSource
Skipper Skipper Skipper defines a function to skip middleware. L17
Rules map[string]string Rules defines the URL path rewrite rules. The values captured in asterisk can be retrieved by index e.g. $1, $2 and so on. Example: "/old": "/new", "/api/*": "/$1", "/js/*": "/public/javascripts/$1", "/users/*/orders/*": "/user/$1/order/$2", Required. L27
RegexRules map[*regexp.Regexp]string RegexRules defines the URL path rewrite rules using regexp.Rexexp with captures Every capture group in the values can be retrieved by index e.g. $1, $2 and so on. Example: "^/old/[0.9]+/": "/new", "^/api/.+?/(.*)": "/v2/$1", L34
Functions from package source
TypeSource
func Rewrite(rules map[string]string) echo.MiddlewareFunc L40
func RewriteWithConfig(config RewriteConfig) echo.MiddlewareFunc L49

Default configuration:

NameValue
SkipperDefaultSkipper

For advanced rewriting of paths, rules may also be defined using regular expressions. Normal capture groups can be defined using () and referenced by index ($1, $2, …) in the rewritten path.

RegexRules and normal Rules can be combined.

e.Pre(middleware.RewriteWithConfig(middleware.RewriteConfig{
Rules: map[string]string{
"^/v1/*": "/v2/$1",
},
RegexRules: map[*regexp.Regexp]string{
regexp.MustCompile("^/foo/([0-9].*)"): "/num/$1",
regexp.MustCompile("^/bar/(.+?)/(.*)"): "/baz/$2/$1",
},
}))