Skip to content

Commit 8369b46

Browse files
committed
Add $request_uri support
1 parent 51dc205 commit 8369b46

3 files changed

Lines changed: 19 additions & 1 deletion

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Create a `direktion.json` config file where you want, with your redirections:
1010
{
1111
"redirects": {
1212
"example.com/blog": {
13-
"location": "https://blog.example.org",
13+
"location": "https://blog.example.org/$request_uri",
1414
"code": 307
1515
},
1616
"example.com": {

direktion.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"net/http"
88
"net/url"
99
"os"
10+
"strings"
1011
)
1112

1213
// Config is the application configuration
@@ -57,16 +58,22 @@ func main() {
5758
func redirectHandler(c *Config) func(w http.ResponseWriter, r *http.Request) {
5859
return func(w http.ResponseWriter, r *http.Request) {
5960
if redirect, exist := c.findRedirect(r.Host, r.URL); exist {
61+
// determinate status code to use
6062
code := http.StatusTemporaryRedirect
6163
if redirect.Code != 0 {
6264
code = redirect.Code
6365
}
6466

67+
// determinate remote ip address to display
6568
remoteIP := r.RemoteAddr
6669
if c.UseXForwarded {
6770
remoteIP = getRealIP(r)
6871
}
6972

73+
// extrapolate variables in location
74+
redirect.Location = strings.Replace(redirect.Location, "$request_uri",
75+
strings.TrimPrefix(r.URL.Path, "/"), 1)
76+
7077
log.Printf("%s - [%d] Redirecting %s%s -> %s", remoteIP, code, r.Host, r.URL.Path, redirect.Location)
7178

7279
w.Header().Add("Location", redirect.Location)

direktion_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,17 @@ func TestRedirection(t *testing.T) {
4848
},
4949
},
5050
},
51+
{
52+
url: "https://creekorful.me/2019/01/12/terminews",
53+
expectedLocation: "https://blog.creekorful.com/2019/01/12/terminews",
54+
expectedCode: 308,
55+
redirects: map[string]Redirect{
56+
"creekorful.me": {
57+
Location: "https://blog.creekorful.com/$request_uri",
58+
Code: 308,
59+
},
60+
},
61+
},
5162
}
5263

5364
for _, test := range tests {

0 commit comments

Comments
 (0)