进阶 www.iris-go.com 2026-10-20 02:39:14 · 14 阅读

第24章 重定向规则 (Redirect Rules)

# 第24章 Redirect Rules 当应用的需求要求把重定向规则集中在一处注册时,可以使用 Rewrite Middleware。它支持基于正则表达式的查找与替换,对 URL 路径、子域名或主机名进行重写。语法对大多数后端开发者来说都很熟悉,如下所示: REDIRECT_CODE_DIGITS PATTERN_REGEX TARGET_REPL 301 /seo/(.*) /$1 上面的规则会用 301(Moved Permanently)HTTP 状态码,把所有来自相对路径 /seo/* 的请求重定向到 /*。想深入了解正则表达式,可参阅相关文档。 ## 用法 首先,按如下方式导入内置中间件: ```go import "github.com/kataras/iris/v12/middleware/rewrite" ``` 有两种方式加载 rewrite 配置项,从而解析并注册重定向规则: 1. 通过 yaml 或 json 文件配合 Load 函数。这是最常见也最简单的场景,解析出错时会直接 panic。 ```go func main() { app := iris.New() // [...routes] redirects := rewrite.Load("redirects.yml") app.WrapRouter(redirects) app.Listen(":80") } ``` 强烈建议在调用 Application.Listen/Run 之前,通过 Application.WrapRouter 将 Rewrite Middleware 注册为 Router Wrapper。 "redirects.yml" 文件的内容如下: ```yaml RedirectMatch: # 将 /seo/* 重定向到 /* - 301 /seo/(.*) /$1 # 将 /docs/v12* 重定向到 /docs - 301 /docs/v12(.*) /docs # 将 /old(.*) 重定向到 / - 301 /old(.*) / # 将 http 或 https://test.* 重定向到 http 或 https://newtest.* - 301 ^(http|https)://test.(.*) $1://newtest.$2 # 将 /*.json 或 .xml 处理为 *?format=json 或 xml, # 不做重定向。参见 /users 路由。 # 当 Code 为 0 时不重定向请求, # 而是修改请求的 URL, # 交给路由处理该请求。 - 0 /(.*).(json|xml) /$1?format=$2 # 将根域名重定向到 www。 # 无需在 Application 中创建 www 子域名, # 所有请求都由根 Application 本身处理。 PrimarySubdomain: www ``` 2. 使用代码调用 New 函数。可以处理解析错误,并能以编程方式存储规则。 (代码块保持原样) ``` go opts := rewrite.Options{ RedirectMatch: []string{ "301 /seo/(.*) /$1", "301 /docs/v12(.*) /docs", "301 /old(.*) /", "301 ^(http|https)://test.(.*) $1://newtest.$2", "0 /(.*).(json|xml) /$1?format=$2", }, PrimarySubdomain: "www", } rw, err := rewrite.New(opts) if err != nil { panic(err) } app.WrapRouter(rw.Rewrite) ``` 示例 让我们写一个简单的应用,它遵循以下重定向规则: 来源 目标 http://mydomain.com:8080/seo/about http://www.mydomain.com:8080/about http://test.mydomain.com:8080 http://newtest.mydomain.com:8080 http://test.mydomain.com:8080/seo/about http://newtest.mydomain.com:8080/about http://mydomain.com:8080/seo http://www.mydomain.com:8080 http://mydomain.com:8080/about http://www.mydomain.com:8080/about http://mydomain.com:8080/docs/v12/hello http://www.mydomain.com:8080/docs http://mydomain.com:8080/docs/v12some http://www.mydomain.com:8080/docs http://mydomain.com:8080/oldsome http://www.mydomain.com:8080 http://mydomain.com:8080/oldindex/random http://www.mydomain.com:8080 http://mydomain.com:8080/users.json http://www.mydomain.com:8080/users?format=json gopackage main import ( "github.com/kataras/iris/v12" "github.com/kataras/iris/v12/middleware/rewrite" ) 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) newtest := app.Subdomain("newtest") newtest.Get("/", newTestIndex) newtest.Get("/", newTestAbout) // redirects := rewrite.Load("redirects.yml") app.WrapRouter(redirects) // app.Listen(":8080") } func index(ctx iris.Context) { ctx.WriteString("Index") } func about(ctx iris.Context) { ctx.WriteString("About") } 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)`) } func newTestIndex(ctx iris.Context) { ctx.WriteString("New Test Subdomain Index") } func newTestAbout(ctx iris.Context) { ctx.WriteString("New Test Subdomain About") } Hosts 文件 ​text 127.0.0.1 mydomain.com 127.0.0.1 www.mydomain.com 127.0.0.1 test.mydomain.com 127.0.0.1 newtest.mydomain.com 1
2
3
4
如果不知道如何修改系统的 hosts 文件,可以点这里查看。

评论 (0)