Mobile app version of vmapp.org
Login or Join
Annie201

: Trying to add cache header to 301 pages made by ModRewrite I have several URLs on my website that get redirected to other URLs using Mod-Rewrite. For example: RewriteRule ^section$ /newsection

@Annie201

Posted in: #301Redirect #302Redirect #HttpHeaders #ModRewrite #Redirects

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 example.com/section to 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) - 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: 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?

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Annie201

1 Comments

Sorted by latest first Latest Oldest Best

 

@Alves908

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.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme