How to set page header cf-cache-status: HIT? - caching

I have set the page header to the public by htaccess file.
<ifModule mod_headers.c>
<filesMatch "\\.(ico|pdf|flv|jpg|jpeg|png|gif|swf|css|js)$">
Header set Cache-Control "max-age=31536000, public"
</filesMatch>
<filesMatch "\\.(html|htm|php)$">
Header set Cache-Control "max-age=31536000, public"
</filesMatch>
</ifModule>
But still, it is showing cf-cache-status: as DYNAMIC for the static HTML page too. Please let me know if there is an additional setting at Cloudflare or the website header.

I'd recommend to review the Default Cache Behaviour documentation page which explains how the Cloudflare Cache works and how to configure it to cache (or bypass caching) different resources. To summarise:
By default only specific file extensions are cached
You can set specific Page Rules to override the default behaviour
You can also use Cloudflare Workers to achieve a customized caching result
You can also use Cache-Control headers, as long as the resource is deemed cacheable (see above)

Related

CORS blocked on some directories, but not others

A week ago, I encountered a CORS error:
Access to XMLHttpRequest at [domainA/example/directory/file.xml] from origin [domainB] has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
I solved this by editing .htaccess, adding:
<IfModule mod_headers.c>
<FilesMatch ".+">
Header set Access-Control-Allow-Origin "*"
</FilesMatch>
</IfModule>
This worked! However, I am now encountering the same problem when requesting data at the same domain, at a different path (i.e. "domainA/different/example/directory/file.json").
Shouldn't I be able to request any file, in any directory, based on the edits I made to .htaccess? (located at "/.../.../www/[domainA-root]/.htaccess")
It's worth noting that in my first example, [domainA/example/directory/file.xml] is a XML view created by Drupal, and is not a static file in an actual directory.
That looks like the typical case in which your browser is caching the response of a previous OPTIONS request to specific URLs requested previously, the header was not there before and the browser won't bother to check again until the TTL expires.
I would say: check it the headers are there doing a verbose curl request from your command line, I.e.
curl -v https://yourhost/yourURL
Then you can take it from there
rather than trying .+ in filematch, you should try:
<FilesMatch "\.(htm|html|css|js|php|json)$">
AddDefaultCharset UTF-8
DefaultLanguage en-US
Header set Access-Control-Allow-Origin "*"
Header set Access-Control-Allow-Methods "*"
</FilesMatch>

Cache browser images if no file extension provided

When a browser requests a jpg and the server returns the data but not as a reference to a jpg file but rather sends the jpg data itself, is it possible for the browser to cache the data?
For example:
http://www.somewebsite.com/image/abc
This uri returns jpg data. Is there some way to get the browser to cache the data it receives to avoid having to request it again?
I just attempted to debug your example but received an Account Suspended. Webmaster, please, contact support. page!
So as I am working blind can I suggest researching the header('Expires: SOME DATA HERE') function in PHP (I assume you are using PHP).
If the solution does not lie there you can also use .htaccess to set the headers using
<IfModule mod_expires.c>
ExpiresActive On
############################################
## Add default Expires header
## http://developer.yahoo.com/performance/rules.html#expires
<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
ExpiresDefault "access plus 1 year"
</FilesMatch>
</IfModule>
This example taken from http://www.robertwent.com/blog/servers-and-hosting/45-set-expires-headers-in-htaccess
Ultimately it is the headers that tell the browser how you advise the content is cached. What the browsers actually does can sometimes be surprising.

Setting up access-control for cross-domain ajax request

I have three sub-domains namely, a.xyz.com, b.xyz.com, c.xyz.com. Now, I have about 20 ajax request to be made on body onload of a.xyz.com.
So, I thought of distributing 20 requests equally among the three domains above. I tried it through this piece of snippet in .htaccess of b.xyz.com and c.xyz.com. However, the request from a.xyz.com to any other sub-domain is still getting dumped.
<IfModule mod_headers.c>
<FilesMatch "\.(php)$">
Header set Access-Control-Allow-Origin: http://a.xyz.com,http://b.xyz.com,http://b.xyz.com
Header set Access-Control-Allow-Methods : POST,GET,OPTIONS
</FilesMatch>
</IfModule>
I have placed the above .htaccess file in my subdomains b.xyz.com and c.xyz.com.
So, can anyone predict whats wrong in my approach ?
Thanks !
Try this to allow cross domain on all xyz.com subdomains:
SetEnvIf Origin "http(s)?://(.+\.)?(xyz\.com)$" ORIGIN_DOMAIN=$0
<FilesMatch "\.(php)$">
<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin %{ORIGIN_DOMAIN}e env=ORIGIN_DOMAIN
Header set Access-Control-Allow-Methods "POST,GET,OPTIONS"
</IfModule>
</FilesMatch>

Trying To "Cookie-Free" A Sub-domain Using .htaccess, But Doesn't Work

Yes, I have searched a lot, here and there, and this is the closest I've found that I thought could actually work. But it doesn't.
<IfModule mod_headers.c>
<FilesMatch "\\.(ico|x-icon|js|css|jpg|jpeg|png|gif|swf|pdf)$">
Header unset Cookie
Header unset Set-Cookie
Header unset ETag
FileETag None
</FilesMatch>
</IfModule>
Pretty clear it should be that I am trying to cookie-free the static files served by a sub-domain, and remove the ETag header on those static files. The ETag thing works fine, but I am unable to unset the cookies.
FYI, Wordpress is installed on example.com (NOT www.example.com; www.example.com redirects to example.com), which is not cookie-free. And i.example.com serves the static files. I want to make i.example.com cookie-free. Is it at all possible to make a subdomain cookie-free when the non-www tld is not cookie-free? (I also see that it's the reason why many suggest using www.tld).
Why you remove the ETag for this files? I would suggest to set this:
FileETag MTime Size
so the browsers, proxies, etc. knows, if a cached file was changed or not.
This speeds up the site a little bit on further visits.
Maybe you set the cookies in the request header so you have to use "RequestHeader unset Cookie" like so:
<IfModule mod_headers.c>
<FilesMatch "\\.(ico|x-icon|js|css|jpg|jpeg|png|gif|swf|pdf)$">
Header set Pragma "public"
Header append Cache-Control "max-age=290304000, public"
RequestHeader unset Cookie
Header unset Cookie
Header unset Set-Cookie
</FilesMatch>
</IfModule>

URL Rewritten and Cache control apache htaccess

I have a page index.php which depending on the arguments provided generates different output.
Using mod_rewrite i have those files a a.htm, b.htm and c.htm .
Now, i want these particular 4 Files (or URLs actually) to be cached locally,
for that i have tried
<FilesMatch "(a\.htm|b\.htm|c\.htm)$">
FileETag None
Header set Cache-Control "max-age=7200, public, must-revalidate"
</FilesMatch>
But it doesn't work.
how do we go about this issue ?
RewriteEngine on
RewriteRule index.htm index.php
<FilesMatch "index.htm">
FileETag None
Header set Cache-Control "max-age=7200, public, must-revalidate"
</FilesMatch>
This is a sneak peak of what i have right now. But in the long run there will be lots of files and rewritten url's. so i want to add cache headers via htaccess . hope it makes sense !

Resources