php.htaccesshttpmetavary

Where and How to set Vary: User-Agent HTTP Header


According to Search Engine Land and many other sources, if you're designing "Dynamic Serving" mobile content for your site, you should set the HTTP header Vary: User-Agent.

Is this done in the .htaccess file or somewhere else? This would be my first time doing this and I would greatly appreciate help and maybe even an example. After searching around, i have narrowed it down to either meta tags or htaccess, however, I could be wrong.

Your help is very much appreciated. Thank you


Solution

  • You do not set this up. It's just that, if you serve different content from the same url based on properties of the client that connects to you (that's what dynamic serving is) you should also return this header, so that search engines know it's not the one true version of the page they are looking at, but just one of the User-Agent dependent versions.

    That way Google can crawl your site using multiple user agents, and cache and index each of them separately, so customers on various platforms are more likely to find the right information.

    You should use this header if you serve different content from the same url depending on the header. So first, you need to build a page that actually has different output based on the user agent, and when you have this, you can optimize it by setting the response header. You can do that by calling the header function in PHP:

    header('Vary: User-Agent');
    

    You can do it in htaccess too, but it's a good idea to only do it for those pages that actually have varying content. So in my opinion, it's just as easy to do it in PHP.