Some users build custom web applications that require checking for a 304 Not Modified header when using the CDN. Let's look at a couple of ways to accomplish that!
Returning a 304 Response requires adding the headers to the Origin file/URL.
Configuring in .htaccess
One method is to manually add the Last Modified
header to .htaccess
:
<IfModule mod_expires.c>
Header set Last-Modified "Mon, 14 Feb 2013 00:00:00 GMT"
</IfModule>
You will have to choose the day you like, this is just an example.
Configuring in PHP
Here is an example doing this within an example PHP script
<?php
$file = "index.php";
$last_modified_time = filemtime($file);
$etag = md5_file($file);
// always send headers
header("Last-Modified: ".gmdate("D, d M Y H:i:s", $last_modified_time)." GMT");
header("Etag: $etag");
// exit if not modified
if (@strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $last_modified_time ||
@trim($_SERVER['HTTP_IF_NONE_MATCH']) == $etag) {
header("HTTP/1.1 304 Not Modified");
exit;
}
?>
The above code is only an example. You can always try different methods, depends on your application and programming platform.
Verifying Functionality
In order to check, you can look at the response header in Firebug's NET Tab, or run the following curl command:
$ curl -I --header 'If-Modified-Since: Mon, 14 Feb 2013 00:00:00 GMT' http://cdn.example.com/myfile.png
HTTP/1.1 304 Not Modified
Date: Wed, 14 Feb 2013 13:58:33 GMT
Connection: keep-alive
ETag: LKJou908hjoiulO879kjlhOIUH
Last-Modified: Fri, 08 Feb 2013 02:25:16 GMT
X-Content-Type-Options: nosniff
Expires: Thu, 14 Feb 2013 13:58:32 GMT
Cache-Control: max-age=86400, s-maxage=604800, must-revalidate, proxy-revalidate
Server: NetDNA-cache/2.2
X-Cache: MISS
304 could be either an X-Cache: HIT or X-Cache: MISS
We hope this article was helpful and as always, If you have any questions or concerns about any of the topics mentioned in this article, please feel free to reach out to support - we are available 24/7 by chat or email!