mirror of
https://github.com/kataras/iris.git
synced 2025-12-27 14:57:05 +00:00
rewrite middleware: add a feature which supports users.json to users?format=json local route redirection
This commit is contained in:
@@ -7,9 +7,11 @@ import (
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
app.Get("/", index)
|
||||
app.Get("/about", about)
|
||||
app.Get("/docs", docs)
|
||||
app.Get("/users", listUsers)
|
||||
|
||||
app.Subdomain("test").Get("/", testIndex)
|
||||
|
||||
@@ -29,6 +31,7 @@ func main() {
|
||||
// http://localhost:8080/docs/v12some -> http://localhost:8080/docs
|
||||
// http://localhost:8080/oldsome -> http://localhost:8080
|
||||
// http://localhost:8080/oldindex/random -> http://localhost:8080
|
||||
// http://localhost:8080/users.json -> http://localhost:8080/users?format=json
|
||||
app.Listen(":8080")
|
||||
}
|
||||
|
||||
@@ -44,8 +47,24 @@ func docs(ctx iris.Context) {
|
||||
ctx.WriteString("Docs")
|
||||
}
|
||||
|
||||
func listUsers(ctx iris.Context) {
|
||||
format := ctx.URLParamDefault("format", "text")
|
||||
/*
|
||||
switch format{
|
||||
case "json":
|
||||
ctx.JSON(response)
|
||||
case "xml":
|
||||
ctx.XML(response)
|
||||
// [...]
|
||||
}
|
||||
*/
|
||||
ctx.Writef("Format: %s", format)
|
||||
}
|
||||
|
||||
func testIndex(ctx iris.Context) {
|
||||
ctx.WriteString("Test Subdomain Index (This should never be executed, redirects to newtest subdomain)")
|
||||
ctx.WriteString(`Test Subdomain Index
|
||||
(This should never be executed,
|
||||
redirects to newtest subdomain)`)
|
||||
}
|
||||
|
||||
func newTestIndex(ctx iris.Context) {
|
||||
@@ -63,6 +82,7 @@ rewriteOptions := rewrite.Options{
|
||||
"301 /docs/v12(.*) /docs",
|
||||
"301 /old(.*) /",
|
||||
"301 ^(http|https)://test.(.*) $1://newtest.$2",
|
||||
"0 /(.*).(json|xml) /$1?format=$2",
|
||||
},
|
||||
PrimarySubdomain: "www",
|
||||
}
|
||||
@@ -77,5 +97,6 @@ app.Use(rewriteEngine.Handler)
|
||||
//
|
||||
// To make the entire application respect the redirect rules
|
||||
// you have to wrap the Iris Router and pass the `Rewrite` method instead
|
||||
// as we did at this example.
|
||||
// as we did at this example:
|
||||
// app.WrapRouter(rewriteEngine.Rewrite)
|
||||
*/
|
||||
|
||||
@@ -1,12 +1,24 @@
|
||||
# REDIRECT_CODE PATH_PATTERN TARGET_PATH_REPL
|
||||
RedirectMatch:
|
||||
# redirects /seo/* to /*
|
||||
RedirectMatch: # REDIRECT_CODE_DIGITS | PATTERN_REGEX | TARGET_REPL
|
||||
# Redirects /seo/* to /*
|
||||
- 301 /seo/(.*) /$1
|
||||
# redirects /docs/v12* to /docs
|
||||
|
||||
# Redirects /docs/v12* to /docs
|
||||
- 301 /docs/v12(.*) /docs
|
||||
# redirects /old(.*) to /
|
||||
|
||||
# Redirects /old(.*) to /
|
||||
- 301 /old(.*) /
|
||||
# redirects http or https://test.* to http or https://newtest.*
|
||||
|
||||
# Redirects http or https://test.* to http or https://newtest.*
|
||||
- 301 ^(http|https)://test.(.*) $1://newtest.$2
|
||||
# redirects root domain to www.
|
||||
PrimarySubdomain: www
|
||||
|
||||
# Handle /*.json or .xml as *?format=json or xml,
|
||||
# without redirect. See /users route.
|
||||
# When Code is 0 then it does not redirect the request,
|
||||
# instead it changes the request URL
|
||||
# and leaves a route handle the request.
|
||||
- 0 /(.*).(json|xml) /$1?format=$2
|
||||
|
||||
# Redirects root domain to www.
|
||||
# Creation of a www subdomain inside the Application is unnecessary,
|
||||
# all requests are handled by the root Application itself.
|
||||
PrimarySubdomain: www
|
||||
Reference in New Issue
Block a user