Making Zope and Plone collaborate with Google Analytics

by admin published 2009/03/26 23:10:00 GMT+0, last modified 2013-06-26T16:00:21+00:00
Otherwise known as the slash-ended problem.

In Apacheland, if you access a folder, Apache automatically redirects you to a new URL ending in a slash (/).  Not so with Plone or Zope -- any resource can or cannot have a slash at the end, which is not nice for neither Web caches nor Google Analytics -- it groups content based on the slash, so the same resource linked twice, once with a slash and once without a slash, those are two separate pages.

The fix

So, how do we fix this?  We fix this with the following Varnish configuration stanza:

sub vcl_recv {
    # Redirect slash-ended requests
    if (req.url != "/" && req.url ~ "/$") {
            set req.http.New-Location = regsub(req.url,"/+$","");
            error 301 "Redirecting you to the non-slash representation...";

sub vcl_error {
    if (req.http.New-Location) {
            set obj.http.Location = req.http.New-Location;

That is it.  The receive routine will detect the URL has a slash at the end, will rewrite the location into a temporary HTTP header, and throw an HTTP error 301.  The error routine will detect the presence of this temporary header and use it to base the Location header for the HTTP error 301 (Moved Permanently) itself.