Skip to content

Instantly share code, notes, and snippets.

@mattdennewitz
Created September 16, 2011 19:39
Show Gist options
  • Select an option

  • Save mattdennewitz/1222935 to your computer and use it in GitHub Desktop.

Select an option

Save mattdennewitz/1222935 to your computer and use it in GitHub Desktop.
VCL Redirect Hack
sub vcl_recv {
# ...
# detect reader's device, redirect if necessary
if (req.http.user-agent ~ "iPhone|iPod|Android") {
if(req.http.host ~ "^example.com") {
# mobile reader is on desktop site.
# raise an error with status=700 and use correct url as response body.
# vcl_error will use this response body as the Location: header.
error 700 "http://m.example.com" + req.url;
}
}
}
sub vcl_error {
# catch custom 700 error, deliver a redirect as response
if(obj.status == 700) {
set obj.status = 302;
# use the response body (the correct url, set in vcl_recv) as the new location
set obj.http.Location = obj.response;
# change the response to a friendly message about the redirect
set obj.response = "Redirecting you to mobile site";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment