URL Rewritten and Cache control apache htaccess - caching

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 !

Related

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

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)

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>

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>

Apache Reverse Caching Proxy - why isn't it caching?

I am attempting to set up a reverse caching proxy for ad graphics (GIF, JPEG, SWF) serving. The proxying is working fine and sending the request on to the origin server, but I can’t seem to figure out why the content isn't being cached. My current config is below. The goal is to cache all requests that match the /ca/ URI prefix. The origin server is serving the files with clean URLs, no file extensions, Cache-control max-age=1 week set on the origin server headers.
ProxyRequests Off
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
<IfModule mod_disk_cache.c>
CacheEnable disk /
CacheRoot "/var/cache/mod_proxy"
CacheDirLevels 3
CacheDirLength 5
CacheIgnoreCacheControl On
</IfModule>
RewriteCond %{REQUEST_URI} ^/ca/*
RewriteRule ^/(.*)$ http://origin.webserver.com/$1 [P,L]
Currently, the only caching I’ve seen actually happen is that of local files accessed on the proxy servers, I’m looking for what I’m missing to get content fetched from the origin server to be cached.
I’m wondering if it has to do with mod_cache not caching because the content is fetched from the origin server, and not on a location on disk.
I am looking for a way to force all requests matching that prefix to be cached.
P.S. It looks like I’m having this exact issue: http://mail-archives.apache.org/mod_mbox/httpd-users/200901.mbox/%3C497F6ED3.7080606#gmail.com%3E. I will be checking my permissions and go over debug messages…
Adding these directives seemed to kick the cache mechanism into gear. I figure it has to do with expiration and cache-control headers as sent from the origin server since I'm serving up images with Symfony/PHP5 instead of directly from the filesystem.
<IfModule mod_disk_cache.c>
CacheEnable disk /
CacheRoot "/var/cache/mod_proxy"
CacheDirLevels 3
CacheDirLength 5
CacheIgnoreCacheControl On
CacheMaxFileSize 100000000
CacheIgnoreNoLastMod On
CacheMaxExpire 1209600
CacheIgnoreQueryString On
</IfModule>

Resources