Skip to content

Commit bde086f

Browse files
committed
add force rewrite support. Closes #181
1 parent d3eaa48 commit bde086f

File tree

4 files changed

+46
-1
lines changed

4 files changed

+46
-1
lines changed

docs/configuration.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,22 @@ A common use-case for rewrites is for SPAs or Single Page Apps, where you want t
362362
}
363363
```
364364

365+
If you wish to force the rewrite regardless of a file existing, set `force` to `true` as shown here:
366+
367+
```json
368+
{
369+
"name": "app",
370+
"type": "static",
371+
"redirects": {
372+
"/*": {
373+
"location": "/",
374+
"status": 200,
375+
"force": true
376+
}
377+
}
378+
}
379+
```
380+
365381
Note that more specific target paths take precedence over those which are less specific, for example `/blog` will win over and `/*`.
366382

367383
## Cross-Origin Resource Sharing

http/redirects/redirects.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,15 @@ func New(c *up.Config, next http.Handler) (http.Handler, error) {
7272
return
7373
}
7474

75+
// forced rewrite
76+
if rule.IsRewrite() && rule.Force {
77+
ctx.WithField("dest", r.URL.Path).Info("forced rewrite")
78+
r.Header.Set("X-Original-Path", r.URL.Path)
79+
r.URL.Path = rule.URL(r.URL.Path)
80+
next.ServeHTTP(w, r)
81+
return
82+
}
83+
7584
// rewrite
7685
if rule.IsRewrite() {
7786
res := &rewrite{ResponseWriter: w}

http/redirects/redirects_test.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ func TestRedirects(t *testing.T) {
4949
Location: "/products/:brand",
5050
Status: 301,
5151
},
52+
"/settings/*": {
53+
Location: "/admin/:splat",
54+
Status: 200,
55+
Force: true,
56+
},
5257
},
5358
}
5459

@@ -62,8 +67,12 @@ func TestRedirects(t *testing.T) {
6267
fmt.Fprintln(w, "products")
6368
case strings.Contains(r.URL.Path, "/docs"):
6469
fmt.Fprintf(w, "docs %s", r.URL.Path)
65-
case strings.Contains(r.URL.Path, "/brand"):
70+
case strings.HasPrefix(r.URL.Path, "/brand"):
6671
fmt.Fprintf(w, "shop %s", r.URL.Path)
72+
case strings.HasPrefix(r.URL.Path, "/setting"):
73+
fmt.Fprintf(w, "settings %s", r.URL.Path)
74+
case strings.HasPrefix(r.URL.Path, "/admin"):
75+
fmt.Fprintf(w, "admin %s", r.URL.Path)
6776
default:
6877
http.NotFound(w, r)
6978
}
@@ -177,4 +186,14 @@ func test(t *testing.T, h http.Handler) {
177186
assert.Equal(t, 301, res.Code)
178187
assert.Equal(t, "Moved Permanently\n", res.Body.String())
179188
})
189+
190+
t.Run("forced rewrite", func(t *testing.T) {
191+
res := httptest.NewRecorder()
192+
req := httptest.NewRequest("GET", "/settings/login", nil)
193+
194+
h.ServeHTTP(res, req)
195+
196+
assert.Equal(t, 200, res.Code)
197+
assert.Equal(t, "admin /admin/login", res.Body.String())
198+
})
180199
}

internal/redirect/redirect.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type Rule struct {
1919
Path string `json:"path"`
2020
Location string `json:"location"`
2121
Status int `json:"status"`
22+
Force bool `json:"force"`
2223
names map[string]bool
2324
dynamic bool
2425
sub string

0 commit comments

Comments
 (0)