How do I handle many site redirects? Follow
There is often a requirement for a site to define many different redirects from one URL to another URL. In a lot of cases, this should be handled at the Origin web server. But what if you want to manage the redirects at Section?
There are two different approaches available for handling many site redirects:
- Configure the redirects in Varnish Cache, this may be as simple as specifying several individual redirects or a more complex series of redirects based on regular expression patterns.
- Using the Section Redirector module which reads a plaintext file made up of 3 parts per line, with each line being its own redirect specification. Note: this module will incur additional costs, reach out to Section for a quote.
Option 1 - Varnish Cache
A simple redirect configuration may look like the following example:
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);
}
}
An example of multiple redirects might look like the following:
sub vcl_recv {
if (req.http.host ~ "(?i)mysite.com") {
return (synth(851, "www.mysite2.com"));
}
if (req.http.host ~ "/hello") {
return (synth(851, "/world"));
}
}
sub vcl_synth {
if (resp.status == 851) {
set resp.http.Location = "https://" + resp.reason + req.url;
set resp.status = 301;
set resp.reason = "Permanent redirect"
return (deliver);
}
}
Option 2 - Section Redirector Module
As previously mentioned, the Section Redirector module will incur additional costs, reach out to Section if you would like a quote. The Section Redirector module reads a plaintext file e.g. "redirects1.txt" and can handle 1-to-1, and wildcard-to-1 style redirects. Note that it does not support wildcard-to-wildcard redirects. However, this can be achieved using Varnish Cache.
The redirects are entered into the plaintext file in the following format:
<request_url> <redirect_url> <status_code>
For example:
test.mysite.com/* https://www.mysite.com/ 301
test.mysite.com/foo/example.png https://www.mysite.com/bar/example.png 302
test.mysite.com https://test2.mysite.com/ 301
- Any requests made to test.mysite.com would be redirected to https://www.mysite.com/ with a status code of 301. Note that this does not preserve the original URL, a request made to test.mysite.com/example.png would be redirected to https://www.mysite.com/. This is an example of a wildcard-to-1 redirect.
- Requests made to test.mysite.com/foo/example.png will redirect to https://www.mysite.com/bar/example.png with a status code of 302. This is an example of a 1-to-1 redirect.
Comments
0 comments
Article is closed for comments.