3

I have several URLs on my website that get redirected to other URLs using Mod-Rewrite. For example:

RewriteRule ^section$ /newsection [NC,L,R=301]

The above rule redirects http://example.com/section to http://example.com/newsection (I'm using example.com here because I don't want to use my real URL as it is irrelevant to my question)

I then proceeded to test the page using webpagetest optimizer at webpagetest.org and The redirect page received and error as follows:

Leverage browser caching of static assets: ##/100

FAILED - (No max-age or expires) - http://example.com/section

So then I proceeded to visit the same page via CURL command line tool and the results are as follows:

Body:

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>
<p>The document has moved <a href="http://example.com/newsection">here</a>.</p>
</body></html>

And headers:

HTTP/1.1 301 Moved Permanently
Date: Sun, 08 Mar 2015 18:09:18 GMT
Server: Apache
Location: http://example.com/newsection
Content-Type: text/html; charset=iso-8859-1

Is there a way that I can add a cache-control or an expires header with a far future date only to pages that return a 301 or 302 status code so that users dont have to make a trip to the network only to load a redirection page?

  • 1
    A bit of an aside... Unless you particularly need fine grained control when caching redirects, webpagetest.orgs "error" would seem to be a bit strict - to the point of being incorrect - if it is indeed refering to the redirect itself and not the target? A 301 redirect is by definition "permanent" and all modern browsers do appear to cache them to some extent (as suggested by RFC 2616). – MrWhite Mar 8 '15 at 21:57
1

You could set an environment variable in the RewriteRule directive and set the Cache-Control header conditionally based on the presence of this environment variable...

RewriteRule ^section$ /newsection [NC,L,R=302,E=cachesection:1]
Header always set Cache-Control "max-age=86400" env=cachesection

...to cache the "temporary" redirect for 1 day.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.