Felix
Felix

Reputation: 159

Varnish 4 rewrite URL transparently

I am looking after a website that is currently running a pretty standard varnish/apache set up. The client needs to add a new domain that transparently serves from a path/query string in order to create a lightweight version of their site. For example:

The user visits mobile.example.com which points to the same server as example.com

Varnish rewrites the mobile.example.com request to example.com/mobile?theme=mobile

User receives the page served from example.com/mobile?theme=mobile by apache, but stays on mobile.example.com

We need to hit both a path and add the query string here, as well as maintain any path the user has entered, i.e: mobile.example.com/test should serve the content at example.com/mobile/test?theme=mobile

Any tips for doing this with Varnish 4? Is it possible?

Upvotes: 2

Views: 2434

Answers (1)

Felix
Felix

Reputation: 159

Got it working!

if (req.http.host ~ "^mobile\.example\.com") {
  set req.http.host = "example.com";
  set req.url = regsub(req.url, "^/", "/mobile/");
  set req.url = regsub(req.url, "$", "?theme=mobile");
} 

Upvotes: 9

Related Questions