Redirect a request

Overview

Varnish Cache allows you to create synthetic HTTP redirect responses with the use of vcl_synth.

Redirect syntax

Below is an example of how to deliver a synthetic redirect 301 response using VCL syntax 4.0. This example will deliver a 301 response with a location header to redirect to “/world” if a request matches the URI path “/hello”

sub vcl_recv {
    if (req.url ~ "/hello") {
        return (synth(801, "/world"));
    }
}

sub vcl_synth {
    if (resp.status == 801) {
        set resp.http.Location = resp.reason;
        set resp.status = 301;
        return (deliver);
    }
}