redirect location <loc> [code <code>] <option> [{if | unless} <condition>] redirect prefix <pfx> [code <code>] <option> [{if | unless} <condition>] redirect scheme <sch> [code <code>] <option> [{if | unless} <condition>] Return an HTTP redirection if/unless a condition is matched 返回一个HTTP redirection if/unless 一个条件匹配,用于下面章节。 May be used in sections : defaults | frontend | listen | backend no | yes | yes | yes If/unless the condition is matched, the HTTP request will lead to a redirect response. If no condition is specified, the redirect applies unconditionally. If/unless 一个条件被匹配, HTTP 请求会指向一个重定向的响应,如果条件被指定,重定向无条件地适用 Arguments : <loc> location With "redirect location", the exact value in <loc> is placed into the HTTP "Location" header. When used in an "http-request" rule, <loc> value follows the log-format rules and can include some dynamic values (see Custom Log Format in section 8.2.4). 重定向的文职, 准确的值 在<loc> 被放置到HTTP "Location" header. 当用在一个 "http-request" 规则,<loc> 值跟着一个log-format 规则可以包含一些动态的值 <pfx> 前缀 With "redirect prefix", the "Location" header is built from the concatenation of <pfx> and the complete URI path, including the query string, unless the "drop-query" option is specified (see below). As a special case, if <pfx> equals exactly "/", then nothing is inserted before the original URI. It allows one to redirect to the same URL (for instance, to insert a cookie). When used in an "http-request" rule, <pfx> value follows the log-format rules and can include some dynamic values (see Custom Log Format in section 8.2.4). 重定向前缀, “Location" header 被构造从 <pfx>关联的事务和完整的URI路径, 包括查询字符串,除非 "drop-query" 选项被指定。 作为一个特别的例子, 如果<pfx> = '/',那么没有什么会被插入在原始的URI.允许重定向到同样的URL <sch> With "redirect scheme", then the "Location" header is built by concatenating <sch> with "://" then the first occurrence of the "Host" header, and then the URI path, including the query string unless the "drop-query" option is specified (see below). If no path is found or if the path is "*", then "/" is used instead. If no "Host" header is found, then an empty host component will be returned, which most recent browsers interpret as redirecting to the same host. This directive is mostly used to redirect HTTP to HTTPS. When used in an "http-request" rule, <sch> value follows the log-format rules and can include some dynamic values (see Custom Log Format in section 8.2.4). <code> The code is optional. It indicates which type of HTTP redirection is desired. Only codes 301, 302, 303, 307 and 308 are supported, with 302 used by default if no code is specified. 301 means "Moved permanently", and a browser may cache the Location. 302 means "Moved permanently" and means that the browser should not cache the redirection. 303 is equivalent to 302 except that the browser will fetch the location with a GET method. 307 is just like 302 but makes it clear that the same method must be reused. Likewise, 308 replaces 301 if the same method must be used. <option> There are several options which can be specified to adjust the expected behaviour of a redirection : - "drop-query" When this keyword is used in a prefix-based redirection, then the location will be set without any possible query-string, which is useful 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 been seen, for instance to protect against some types of DoS. No other cookie option is added, so the cookie will be a session cookie. Note that for a browser, a sole cookie name without an equal sign is different from a cookie with an equal sign. - "clear-cookie NAME[=]" A "Set-Cookie" header will be added with NAME (and optionally "="), but with the "Max-Age" attribute set to zero. This will tell the browser to delete this cookie. It is useful for instance on logout pages. It is important to note that clearing the cookie "NAME" will not remove a cookie set with "NAME=value". You have to clear the cookie "NAME=" for that, because the browser makes the difference. Example: move the login URL only to HTTPS. acl clear dst_port 80 acl secure dst_port 8080 acl login_page url_beg /login acl logout url_beg /logout acl uid_given url_reg /login?userid=[^&]+ acl cookie_set hdr_sub(cookie) SEEN=1 redirect prefix https://mysite.com set-cookie SEEN=1 if !cookie_set redirect prefix https://mysite.com if login_page !secure redirect prefix http://mysite.com drop-query if login_page !uid_given 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 Example: redirect all HTTP traffic to HTTPS when SSL is handled by haproxy. redirect scheme https if !{ ssl_fc } 重定向所有的HTTP 到HTTPS Example: append 'www.' prefix in front of all hosts not having it http-request redirect code 301 location www.%[hdr(host)]%[req.uri] \ unless { hdr_beg(host) -i www } 追加www. 前缀在所有的主机名前 没有http-request 重定向代码301 location www.%[hdr(host)]%[req.uri] 除非 { hdr_beg(host) -i www } See section 7 about ACL usage.