You can change the URL of a request before any processing takes place this is sometimes useful for aliasing routes or when a route is move and it would be nice to still handle the old routes.

To rewrite a URL the server provides a Rewrite function that uses regular expression to match and transform the URL.

Configuration

This function expects a RewriteRules map to be passed where the keys are regular expressions patterns and the values are the finalized URLs. The final URLs can also use the value of regular expression capture groups with the $1,$2,$3, values.

rules := RewriteRules {
  // /people/:id/profile -> /users/:id/profile
  "/people/(.*)/profile": "/users/$1/profile",
}

Usage

svr := celerity.New()

svr.Pre(middelware.RewriteRules{
  "/people/(.*)/profile": "/users/$1/profile",
})

NOTE: Rewrite middleware must be loaded as preroute middleware using the Pre function and not the standard Use function.

last modified Tuesday, July 3, 2018
Previous in Middlewares
CORS
Adds CORS response headers to the responses.
Celerity is maintained by 5Sigma. Source code is available at Github.