HAProxy Enterprise Documentation 2.3r1

Redirects

Use the http-request redirect configuration directive to reroute HTTP traffic. These send back an HTTP redirect response to the client and then the client makes a new request to the new resource. When performing a redirection, HAProxy Enterprise responds directly to the client; it does not forward any traffic to the server.

You can specify the HTTP status code to return by setting the code parameter. Use any of the following:

Code

Meaning

301

Permanent move

302

Temporary move; should not be cached by the client. This is the default value if no code is configured.

303

Similar to 302, but the browser must fetch the new location using a GET

307

Similar to 302, but the browser must reuse the same method as the one from the original request

308

Similar to 301, but the browser must reuse the same method as the one from the original request

Redirect traffic to a location

Use http-request redirect location to replace the entire location of a URL. Below, we append a www. prefix in front of all URLs that do not have it. Notice that you can use code to set the HTTP status code:

frontend www
   bind :80
   acl has_www hdr_beg(host) -i www
   http-request redirect  code 301  location http://www.%[hdr(host)]%[capture.req.uri]  unless has_www
   use_backend webservers

Redirect traffic using a prefix

Use http-request redirect prefix to add a prefix to the URL's location. Below, we prefix all URLs with /api/v2/ if they don't have it:

frontend www
   bind :80
   acl begins_with_api path_beg /api/v2/
   http-request redirect code 301 prefix /api/v2 unless begins_with_api
   use_backend webservers

Notice that you do not need to end the prefix with a slash. One will be added to the end of the prefix automatically.

Redirect the scheme

Use http-request redirect scheme to redirect to a different scheme, such as from http:// to https:://. In the following example, we redirect all HTTP traffic to HTTPS when SSL is handled by HAProxy Enterprise:

frontend www
   bind :80
   acl is_https ssl_fc
   http-request redirect scheme https unless is_https
   use_backend webservers

The Location header is built by concatenating the following elements in this order:

  • the scheme (e.g. https) provided by the directive

  • ://

  • the first occurence of the Host header

  • the URL

Options

All http-request redirect directives support the following options:

drop-query

Removes the query string from the original URL when performing the concatenation.

append-slash

Used in conjunction with drop-query to add a / character at the end of the URL

set-cookie NAME[=value]

Adds a Set-Cookie header to the redirection. The cookie is named NAME and can have an optional value.

clear-cookie NAME[=]

A special Set-Cookie header is added to the redirection. The cookie is named NAME and the Max-Age cookie parameter is set to 0. Its purpose is to instruct the browser to delete the cookie.

Note

To a browser, these are two different cookies: NAME and NAME=. You must adapt the two statements above based on your traffic pattern.


Next up

Rewrites