Rewrite Middleware
When the Application's requirements suggests that the redirect rules should be registered in one place then use the Rewrite Middleware. The Rewrite Middleware supports rewrite URL path, subdomain or host based on a regular expression search and replace.
The syntax is familiar to the majority of the backend developers out there and it looks like that:
REDIRECT_CODE_DIGITS
PATTERN_REGEX
TARGET_REPL
301
/seo/(.*)
/$1
The above will redirect all requests from relative path /seo/* to /* using the 301 (Moved Permanently) HTTP Status Code. Learn more about regex.
Usage
First of all, you should import the builtin middleware as follows:
1
import "github.com/kataras/iris/v12/middleware/rewrite"
Copied!
There are two ways to load rewrite options in order to parse and register the redirect rules:
1. Through a yaml or json file using the Load function. It is the most common scenario and the simplest one. It panics on parse errors.
1
func main() {
2
app := iris.New()
3
// [...routes]
4
redirects := rewrite.Load("redirects.yml")
5
app.WrapRouter(redirects)
6
app.Listen(":80")
7
}
Copied!
It is highly recommended that you should register the Rewrite Middleware as a Router Wrapper through Application.WrapRouter right before Application.Listen/Run.
The "redirects.yml" file looks like that:
1
RedirectMatch:
2
# Redirects /seo/* to /*
3
- 301 /seo/(.*) /$1
4
5
# Redirects /docs/v12* to /docs
6
- 301 /docs/v12(.*) /docs
7
8
# Redirects /old(.*) to /
9
- 301 /old(.*) /
10
11
# Redirects http or https://test.* to http or https://newtest.*
12
- 301 ^(http|https)://test.(.*) $1://newtest.$2
13
14
# Handles /*.json or .xml as *?format=json or xml,
15
# without redirect. See /users route.
16
# When Code is 0 then it does not redirect the request,
17
# instead it changes the request URL
18
# and leaves a route handle the request.
19
- 0 /(.*).(json|xml) /$1?format=$2
20
21
# Redirects root domain to www.
22
# Creation of a www subdomain inside the Application is unnecessary,
23
# all requests are handled by the root Application itself.
24
PrimarySubdomain: www
Copied!
2. Through code using the New function. Parse errors can be handled and rules can be programmatically stored.
1
opts := rewrite.Options{
2
RedirectMatch: []string{
3
"301 /seo/(.*) /$1",
4
"301 /docs/v12(.*) /docs",
5
"301 /old(.*) /",
6
"301 ^(http|https)://test.(.*) $1://newtest.$2",
7
"0 /(.*).(json|xml) /$1?format=$2",
8
},
9
PrimarySubdomain: "www",
10
}
11
rw, err := rewrite.New(opts)
12
if err != nil { panic(err) }
13
app.WrapRouter(rw.Rewrite)
Copied!

Example

Let's write a simple application which follows the redirect rules of:
1
package main
2
3
import (
4
"github.com/kataras/iris/v12"
5
"github.com/kataras/iris/v12/middleware/rewrite"
6
)
7
8
func main() {
9
app := iris.New()
10
11
app.Get("/", index)
12
app.Get("/about", about)
13
app.Get("/docs", docs)
14
app.Get("/users", listUsers)
15
16
app.Subdomain("test").Get("/", testIndex)
17
18
newtest := app.Subdomain("newtest")
19
newtest.Get("/", newTestIndex)
20
newtest.Get("/", newTestAbout)
21
22
//
23
redirects := rewrite.Load("redirects.yml")
24
app.WrapRouter(redirects)
25
//
26
27
app.Listen(":8080")
28
}
29
30
func index(ctx iris.Context) {
31
ctx.WriteString("Index")
32
}
33
34
func about(ctx iris.Context) {
35
ctx.WriteString("About")
36
}
37
38
func docs(ctx iris.Context) {
39
ctx.WriteString("Docs")
40
}
41
42
func listUsers(ctx iris.Context) {
43
format := ctx.URLParamDefault("format", "text")
44
/*
45
switch format{
46
case "json":
47
ctx.JSON(response)
48
case "xml":
49
ctx.XML(response)
50
// [...]
51
}
52
*/
53
ctx.Writef("Format: %s", format)
54
}
55
56
func testIndex(ctx iris.Context) {
57
ctx.WriteString(`Test Subdomain Index
58
(This should never be executed,
59
redirects to newtest subdomain)`)
60
}
61
62
func newTestIndex(ctx iris.Context) {
63
ctx.WriteString("New Test Subdomain Index")
64
}
65
66
func newTestAbout(ctx iris.Context) {
67
ctx.WriteString("New Test Subdomain About")
68
}
Copied!

Hosts File

1
127.0.0.1 mydomain.com
2
127.0.0.1 www.mydomain.com
3
127.0.0.1 test.mydomain.com
4
127.0.0.1 newtest.mydomain.com
Copied!
Navigate here if you don't know how to modify the system's hosts file.
Last modified 10mo ago
Export as PDF
Copy link