]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
[MINOR] http redirect: add the ability to append a '/' to the URL
authorWilly Tarreau <w@1wt.eu>
Sat, 9 Jan 2010 23:42:19 +0000 (00:42 +0100)
committerWilly Tarreau <w@1wt.eu>
Sat, 9 Jan 2010 23:42:19 +0000 (00:42 +0100)
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.

doc/configuration.txt
include/types/proto_http.h
src/cfgparse.c
src/proto_http.c

index 2440b1b0d929f471209ba0c6f4e9766ccf814118..528b991e03331ffc692ce5549e12466150030bfc 100644 (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.
 
 
index ad5d76a620887091e02f96f5ac15c602f5f44b57..a0622b7de720e693d7d3665feec732c5289f774f 100644 (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 ) */
index 893e16e936e14f1d8f6e7f2ad9747f851e96093c..58505ec27f95c6e0e0248a413a22e6ed009062ec 100644 (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;
index 75dd62a9450da488b15aa92b841a57a3dec7433e..fa8f5576822bf93f9c4e356d49c19368175974da 100644 (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: