[MINOR] http redirect: add the ability to append a '/' to the URL

Sometimes it can be desired to return a location which is the same
as the request with a slash appended when there was not one in the
request. A typical use of this is for sending a 301 so that people
don't reference links without the trailing slash. The name of the
new option is "append-slash" and it can be used on "redirect"
statements in prefix mode.
This commit is contained in:
Willy Tarreau 2010-01-10 00:42:19 +01:00
parent dcb75c4a83
commit 81e3b4f48d
4 changed files with 25 additions and 1 deletions

View File

@ -3354,6 +3354,12 @@ redirect prefix <to> [code <code>] <option> [{if | unless} <condition>]
for directing users to a non-secure page for instance. It has no effect
with a location-type redirect.
- "append-slash"
This keyword may be used in conjunction with "drop-query" to redirect
users who use a URL not ending with a '/' to the same one with the '/'.
It can be useful to ensure that search engines will only see one URL.
For this, a return code 301 is preferred.
- "set-cookie NAME[=value]"
A "Set-Cookie" header will be added with NAME (and optionally "=value")
to the response. This is sometimes used to indicate that a user has
@ -3384,6 +3390,10 @@ redirect prefix <to> [code <code>] <option> [{if | unless} <condition>]
redirect location http://mysite.com/ if !login_page secure
redirect location / clear-cookie USERID= if logout
Example: send redirects for request for articles without a '/'.
acl missing_slash path_reg ^/article/[^/]*$
redirect code 301 prefix / drop-query append-slash if missing_slash
See section 7 about ACL usage.

View File

@ -225,6 +225,7 @@ enum {
enum {
REDIRECT_FLAG_NONE = 0,
REDIRECT_FLAG_DROP_QS = 1, /* drop query string */
REDIRECT_FLAG_APPEND_SLASH = 2, /* append a slash if missing at the end */
};
/* Redirect types (location, prefix, extended ) */

View File

@ -1848,6 +1848,9 @@ int cfg_parse_listen(const char *file, int linenum, char **args, int kwm)
else if (!strcmp(args[cur_arg],"drop-query")) {
flags |= REDIRECT_FLAG_DROP_QS;
}
else if (!strcmp(args[cur_arg],"append-slash")) {
flags |= REDIRECT_FLAG_APPEND_SLASH;
}
else if (!strcmp(args[cur_arg], "if")) {
pol = ACL_COND_IF;
cur_arg++;
@ -1859,7 +1862,7 @@ int cfg_parse_listen(const char *file, int linenum, char **args, int kwm)
break;
}
else {
Alert("parsing [%s:%d] : '%s' expects 'code', 'prefix', 'location', 'set-cookie', 'clear-cookie' or 'drop-query' (was '%s').\n",
Alert("parsing [%s:%d] : '%s' expects 'code', 'prefix', 'location', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s').\n",
file, linenum, args[0], args[cur_arg]);
err_code |= ERR_ALERT | ERR_FATAL;
goto out;

View File

@ -2820,6 +2820,16 @@ int http_process_req_common(struct session *s, struct buffer *req, int an_bit, s
/* add path */
memcpy(rdr.str + rdr.len, path, pathlen);
rdr.len += pathlen;
/* append a slash at the end of the location is needed and missing */
if (rdr.len && rdr.str[rdr.len - 1] != '/' &&
(rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
if (rdr.len > rdr.size - 5)
goto return_bad_req;
rdr.str[rdr.len] = '/';
rdr.len++;
}
break;
}
case REDIRECT_TYPE_LOCATION: