How to redirect non-www URLs to www in Varnish

  • by

If a website's canonical URL has www, it is desirable, as a good SEO practice, to redirect the non-www URLs to www. That is, if the canonical URL is www.example.com, example.com should be redirected to www.example.com. How to do this when Varnish is listening on port 80 as a reverse HTTP proxy is given below in this post.

Solution

For Varnish 3.0

Add the following code in the file, /etc/varnish/default.vcl,

sub vcl_recv {
    if (req.http.host == "example.com") {
        set req.http.host = "www.example.com";
        error 750 "http://" + req.http.host + req.url;
    }
}

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

In the above code, you need to replace the two occurrences of example.com with the domain name of your website.

For Varnish 4.0

There are changes in VCL for Varnish 4.0, and the code to be added to /etc/varnish/default.vcl is,

sub vcl_recv {
    if (req.http.host ~ "^example.com") {
        return (synth (750, ""));
    }
}

sub vcl_synth {
    if (resp.status == 750) {
        set resp.status = 301;
        set resp.http.Location = "http://www.example.com" + req.url;
        return(deliver);
    }
}

As mentioned before, the two occurrences of example.com in the above code need to be replaced with the domain name of your website.

Subscribe
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

You may like these, also

  • POSIX message queues in LinuxPOSIX message queues in Linux 1.0 POSIX Message queues POSIX interprocess comunication (IPC) was introduced in the POSIX.1b standard (IEEE Std 1003.1b-1993) for real time…
  • POSIX Shared Memory in LinuxPOSIX Shared Memory in Linux 1.0 Shared Memory Shared memory is the fastest method of interprocess communication (IPC) under Linux and other Unix-like systems. The…
  • POSIX Semaphores in LinuxPOSIX Semaphores in Linux 1.0 Semaphores Semaphores are used for process and thread synchronization. Semaphores are clubbed with message queues and shared memory under…
  • fork and exec system calls in Linuxfork and exec system calls in Linux 1.0 fork and exec system calls Suppose we wish to write a "shell program" which would execute another program. Now,…
  • Connecting two computers with Ethernet LAN cableConnecting two computers with Ethernet LAN cable Quite often, we wish to connect two computers back to back using an Ethernet LAN cable. It may be because…
  • D-Bus TutorialD-Bus Tutorial 1.0 D-Bus D-Bus is a mechanism for interprocess communication under Linux and other Unix-like systems. D-Bus has a layered architecture.…
  • Socket programming using the select system callSocket programming using the select system call 1.0 Client-Server Paradigm The Client-Server paradigm divides the software architecture of a system in two parts, the server and its…
  • System V message queues in LinuxSystem V message queues in Linux 1.0 Message queues Message queues are one of the interprocess communication mechanisms available under Linux. Message queues, shared memory and…
  • POSIX Threads Synchronization in CPOSIX Threads Synchronization in C 1.0 POSIX Threads Synchronization POSIX Threads provide multiple flows of execution within a process. The threads have their own stacks…
  • System V Shared Memory in LinuxSystem V Shared Memory in Linux 1.0 Shared Memory Shared memory is one of the three interprocess communication (IPC) mechanisms available under Linux and other Unix-like…