[MINOR] redirect: in prefix mode a "/" means not to change the URI
If the prefix is set to "/", it means the user does not want to alter
the original URI, so we don't want to insert a new slash before the
original URI.
(cherry-picked from commit 02a35c74942c1bce762e996698add1270e6a5030)
diff --git a/doc/configuration.txt b/doc/configuration.txt
index ca6f293..f92176a 100644
--- a/doc/configuration.txt
+++ b/doc/configuration.txt
@@ -2350,7 +2350,10 @@
the HTTP "Location" header. In case of "redirect prefix", the
"Location" header is built from the concatenation of <to> and the
complete URI, including the query string, unless the "drop-query"
- option is specified (see below).
+ option is specified (see below). As a special case, if <to>
+ equals exactly "/" in prefix mode, then nothing is inserted
+ before the original URI. It allows one to redirect to the same
+ URL.
<code> The code is optional. It indicates which type of HTTP redirection
is desired. Only codes 301, 302 and 303 are supported, and 302 is
diff --git a/src/proto_http.c b/src/proto_http.c
index 2b72504..ecbc887 100644
--- a/src/proto_http.c
+++ b/src/proto_http.c
@@ -1912,9 +1912,14 @@
if (rdr.len + rule->rdr_len + pathlen > sizeof(trash) - 4)
goto return_bad_req;
- /* add prefix */
- memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
- rdr.len += rule->rdr_len;
+ /* add prefix. Note that if prefix == "/", we don't want to
+ * add anything, otherwise it makes it hard for the user to
+ * configure a self-redirection.
+ */
+ if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
+ memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
+ rdr.len += rule->rdr_len;
+ }
/* add path */
memcpy(rdr.str + rdr.len, path, pathlen);
diff --git a/tests/test-redirect.cfg b/tests/test-redirect.cfg
index 780132b..582a069 100644
--- a/tests/test-redirect.cfg
+++ b/tests/test-redirect.cfg
@@ -26,8 +26,8 @@
redirect prefix /pfx/test code 302 if url_test2
redirect prefix /pfx/test code 303 drop-query if url_test3
- redirect location /test4 code 302 set-cookie SEEN=1 if url_test4 !seen
- redirect location / code 302 clear-cookie SEEN= if url_test4 seen
+ redirect prefix / code 302 set-cookie SEEN=1 if url_test4 !seen
+ redirect location / code 302 clear-cookie SEEN= if url_test4 seen
### unconditional redirection
#redirect location https://example.com/ if TRUE