4

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

NotJay
  • 3,919
  • 5
  • 38
  • 62
  • Up vote for sourcing your claim. There hasn't been a lot of implementation on the topic and a through blog post would be a big help to new / intermediate webmasters and developers. – berg37 Feb 09 '16 at 13:55

2 Answers2

3

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.

GolezTrol
  • 114,394
  • 18
  • 182
  • 210
  • 1
    Great. Should I be able to see the Vary: User-Agent when I check the http headers with a service like http://www.delorie.com/web/headers.html ? I added it to the php code and tested it, however, it was not appearing. How else would I be able to test this? – NotJay Aug 27 '14 at 19:13
  • 1
    Open Chrome, press F12 (Dev tools will open), go to the network tab. Now open your page (in the normal address bar), and you can inspect the request in the Network tab. Just select the main request for the page and details of it will show up, including the response headers. – GolezTrol Aug 27 '14 at 19:22
2

If you’re using PHP to generate your pages, you can use the header function, like this:

header('Vary: User-Agent');

Or, if you’re on an apache server, you can use the mod_headers module. Like this:

Header: set Vary User-Agent

Note that if you send different content based on other headers too, then you should append these other headers to the Vary header of your response.

Étienne Miret
  • 6,448
  • 5
  • 24
  • 36
  • Although this is an Apache question, if you came here whilst looking for a solution in ASP.NET/IIS equivalent, the answer is.... Response.AppendHeader("Vary", "User-Agent"); – MiscellaneousUser May 03 '15 at 10:21