Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a Drupal site that uses Varnish. I am trying to redirect people that come to a url starting with /node when they view the site via Varnish.

I have tried the following in the .htaccess file but it gives 500 errors.

RewriteCond %{HTTP:Via} =1.1 varnish
RewriteRule ^node / [R=301,NC,L]

I haven't been able to find anything using google in relation to how to target the 'Via' section of a header.

Cheers, Paul

share|improve this question

2 Answers 2

I don't have Varnish installed here to test with, but it seems to me you're not escaping the plain text in your RewriteCond. The pattern is always considered to be a regex, so spaces and periods have special meaning. Try:

RewriteCond %{HTTP:Via} =1\.1\ varnish
RewriteRule ^node / [R=301,NC,L]

As an aside, this is a bit of a messy way to do things. Normally you would check the X-Forwarded-By header to determine if you're behind a proxy. As these can be spoofed and/or added by upstream proxies, you should either:

  1. In the varnish VCL, remove all X-Forwarded-By headers from the request and re-add your own OR
  2. add in a custom header (say: X-Real-Forwarded-By) and check for it's existence in your .htaccess
share|improve this answer
    
Cheers for your answer and explanation, I am checking X-Forwarded as well but I only put the HTTP:Via one in my question because it was giving me problems. –  P4ul Apr 12 '12 at 23:25
    
Finally tried this out but I can't get this to work.. –  P4ul May 4 '12 at 1:07

Because Varnish isn't intended to do that you have to use a little trick for that.

For sending out a HTTP redirect the best way is to prepare a specific error handler and to raise this error handler depending on your requirements.

Step 1) Prepare an error handler to send a redirect

sub vcl_error {
  ...
  # redirect
  if (obj.status == 750) {
    set obj.status = 301;
    set obj.http.Location = "http://www.error-location.com/";
    return (deliver);
  }
  ...
}

Step 2) Raise prepared error handler

sub vcl_recv {
  ...
  # raise error if someone requests /node
  if (req.url ~ "^/node") {
    error 750;
  }
  ...
}

Alternatively you can set-up the error handler to use a dynamic target

sub vcl_error {
  ...
  # redirect to dynamic target
  if (obj.status == 750) {
    set obj.status = 301;
    set obj.http.Location = obj.response;
    return (deliver);
  }
  ...
}

...

sub vcl_recv {
  ...
  # redirect, if someone requests /node
  if (req.url ~ "^/node") {
    error 750 "http://www.error-location.com/";
  }
  ...
}
share|improve this answer
    
Thanks for your answer but I don't have access to varnish so I will stick to the .htaccess solution above. –  P4ul Apr 12 '12 at 23:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.