How to do a cacheable redirection?

Posted by John Doe on Pro Webmasters See other posts from Pro Webmasters or by John Doe
Published on 2012-11-29T05:01:46Z Indexed on 2012/11/29 5:21 UTC
Read the original article Hit count: 348

Filed under:
|
|

When users enter my website example.com, their "preferred" language is detected and they are redirected (using a 301 Moved Permanently redirection) to example.com/en/ (for english), example.com/it/ (for italian), etc.

It works perfectly, but when I analized my website with the Google Page Speed tool it gave me the following advice.

Many pages, especially mobile pages, redirect users to a different URL, for instance from www.example.com to m.example.com. Making this redirect cacheable by the user's browser can speed up page load times for repeat visitors to a site.

And later it says

We recommend using a 302 redirect with a cache lifetime of one day. The redirect should include a Vary: User-Agent header as well as a Cache-Control: private header.

So my questions are, how can I do a "cacheable" redirection in PHP? Would the following be enough?

header("HTTP/1.0 302 Moved Temporarily");
header("Location: example.com/whatever");
exit;

© Pro Webmasters or respective owner

Related posts about php

Related posts about redirects