3

I have a relatively simple (I think) use-case but I can't find any examples where someone has done this. We are using Varnish as a cache and reverse proxy in front of two different applications and would like to make things a bit more unified across both as they both do similar things. I was hoping Varnish could help rewrite the URLs as shown below.

Original application URL for pagination (get first 10 items):

//myapplication.com/products/?startindex=1&endindex=10 Desired URL:

//myapplication.com/products/?paginate=1:10 This is just one example (the most complex because it combines two parameters), but in all cases the input values for the parameters stay the same, it is just that the parameter names will change.

Another example would be:

//myapplication.com/search/?query=something to:

//myapplication.com/search/?q=something Does anyone have any experience with varnish and how this could be done?

Thanks

1 Answer 1

3

If you want to internally rewrite the URLs, put the following (untested) in vcl_recv:

set req.url = regsub(
    req.url,
    "^/products/.*([&?]startindex=([0-9]+)|[&?]endindex=([0-9]+))*",
    "/products/?paginate=\2:\3"
);

If you want to 301 requests from one URL style to the other then it's a little more complicated, but here's a rough start on how you might do it:

vcl_recv:

set req.http.X-Redirect-URL = regsub(
    req.url,
    "^/products/.*([&?]startindex=([0-9]+)|[&?]endindex=([0-9]+))*",
    "/products/?paginate=\2:\3"
);
error 700 req.http.X-Redirect-URL;

vcl_error:

if (obj.status == 700) {
    set obj.http.Location = obj.response;
    set obj.status = 301;
    set obj.response = "Moved Permanently";
}

Obviously, if you have other parameters you're going to have to jump through a few more hoops to ensure that they get correctly passed through.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .