Created
September 16, 2011 19:39
-
-
Save mattdennewitz/1222935 to your computer and use it in GitHub Desktop.
VCL Redirect Hack
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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