Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

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 analyzed 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 cache-able 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 "cache-able" redirection in PHP? Would the following be enough?

header("HTTP/1.0 302 Moved Temporarily");
header("Location: example.com/whatever");
exit;
share|improve this question

1 Answer 1

up vote 4 down vote accepted

This should do the trick:

header("HTTP/1.0 302 Moved Temporarily");
header("Location: example.com/whatever");
header("Cache-Control: private");
header("Vary: User-Agent, Accept-Encoding");
exit;

The recommendation for the Vary header is from this google developer page about optimizing caches (and problems with some IE < 9). Background on caching negotiated responses from RFC2616 (Header Field Definitions) where you will also find background on the Cache-control-private. A further discussion about the Vary: Header also amongst others on this stackoverflow page.

You might also find useful: Multi-regional and multilingual sites and working with multilingual websites.

share|improve this answer
    
Responding to the specific question + documentation to back it up: great answer man. Thank you very much –  fedeetz Nov 29 '12 at 10:07

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.