can't cache static files using htaccess - php

I am new to caching and i am trying to test how files would be cached so i have created a simple php web page to test if i can cache the css file:
<!doctype html>
<head>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1 id="heading">Test</h1>
</body>
</html>
and i have created an htaccess file then added these lines from here
# ----------------------------------------------------------------------
# | Expires headers |
# ----------------------------------------------------------------------
# Serve resources with far-future expires headers.
#
# (!) If you don't control versioning with filename-based
# cache busting, you should consider lowering the cache times
# to something like one week.
#
# https://httpd.apache.org/docs/current/mod/mod_expires.html
<IfModule mod_expires.c>
ExpiresActive on
ExpiresDefault "access plus 1 month"
# CSS
ExpiresByType text/css "access plus 1 year"
</IfModule>
then i tried to change style to check if it is cached but style changes on every load which means that there is no caching, then i thought my be it is the local server configurations so i moved to live one and agian still the same and it tested headers with this site
and i got this:
HTTP/1.1 200 OK
Server: nginx/1.12.1
Date: Sun, 24 Sep 2017 13:02:51 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Cache-Control: max-age=0
Expires: Sun, 24 Sep 2017 13:02:51 GMT
Vary: Accept-Encoding,User-Agent
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
EDIT:
I have found that mode_expires was not active on localhost and i enabled it but still facing the problem

try adding this part :
<IfModule mod_headers.c>
<FilesMatch "\.(ico|jpe?g|png|gif|swf|css|gz)$">
Header set Cache-Control "max-age=2592000, public"
</FilesMatch>
<FilesMatch "\.(js)$">
Header set Cache-Control "max-age=2592000, private"
</FilesMatch>
<filesMatch "\.(html|htm)$">
Header set Cache-Control "max-age=7200, public"
</filesMatch>
# Disable caching for scripts and other dynamic files
<FilesMatch "\.(pl|php|cgi|spl|scgi|fcgi)$">
Header unset Cache-Control
</FilesMatch>
</IfModule>

Related

I'm not able to cache my PHP rewritten CSS file

I have a php file that is renamed in htaccess as a css file. The reason being, is because I have some styles and colors which change based on some admin options.
Anyway, I am trying to allow a visitor's browser to cache the file. Here's what I have:
style.php:
header("Content-type: text/css; charset: UTF-8");
// Start normal CSS styles...
.htaccess:
RewriteRule ^assets/css/min/([a-zA-Z0-9\._-]+)/([a-zA-Z0-9\._-]+)/([a-zA-Z0-9\._-]+)\.css$ assets/css/min.php?style=$1&layout=$2&ver=$3 [L,QSA]
# Compress
AddOutputFilterByType DEFLATE text/css
# Cache for 1 week
<FilesMatch ".(css)$">
Header set Cache-Control "max-age=604800"
</FilesMatch>
<IfModule mod_expires.c>
ExpiresActive on
ExpiresByType text/css "access plus 1 week"
</IfModule>
In the page head:
<link rel="stylesheet" href="http://example.com/assets/css/min/blue/flat/0.9.2.css">
Each time a new page is loaded, the page contents are requested. These are the headers I receive:
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection: Keep-Alive
Content-Encoding: gzip
Content-Length: 13881
Content-Type: text/css; charset: UTF-8;charset=UTF-8
Date: Sat, 20 Feb 2016 22:49:17 GMT
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Keep-Alive: timeout=5, max=94
Pragma: no-cache
Server: Apache/2.4.16 (Unix) OpenSSL/1.0.1p PHP/5.6.12 mod_perl/2.0.8-dev Perl/v5.16.3
Vary: Accept-Encoding,User-Agent
X-Powered-By: PHP/5.6.12
Your mod_expires config applies to files based on their mimetype as perceived by the webserver. Unless you've done some major surgery on the webserver config elsewhere, the webserver will not associate the text/CSS mimetype with files ending in .php (and if it did, much further hacking would be required to get them processed by the PHP parser). The header set by your PHP script is irrelevant to this process.
While it is possible to force mod_expires to add caching headers to the response, you'll also have to use mod_headers to remove the values PHP sets; when a browsers receives multiple conflicting caching instructions, it will resort to the shortest expiry time expressed in them.
Hence, to make the content cacheable, you should emit the caching information directly from the PHP script. E.g.
header('Cache-control: max-age=604800; private');
But basing the expiry time on the access time is not the best solution.

PHP server (Apache) only compresses JSON output with ob_start

So I’ve setup a web server for a REST API and I’m coding it in PHP.
I’ve set Apache so that I can use gzip compression:
<IfModule mod_mime.c>
AddType application/x-javascript .js
AddType text/css .css
</IfModule>
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript application/json
<IfModule mod_setenvif.c>
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
</IfModule>
<IfModule mod_headers.c>
Header append Vary User-Agent env=!dont-vary
</IfModule>
</IfModule>
Then using curl I test which headers I are sent back:
HTTP/1.1 200 OK
Date: Sat, 24 Oct 2015 23:24:34 GMT
Server: Apache/2.4.10 (Unix) OpenSSL/1.0.1j PHP/5.6.3 mod_perl/2.0.8-dev Perl/v5.16.3
Last-Modified: Sat, 24 Oct 2015 23:04:13 GMT
ETag: "b9413-522e1bf1eb540-gzip"
Accept-Ranges: bytes
Vary: Accept-Encoding,User-Agent
Content-Encoding: gzip
Content-Length: 4204
Content-Type: text/plain
Great, that seems to work. However, when I run my test script (index.php) which creates a resource and returns the object (or error) in a JSON response, I get the following:
HTTP/1.1 201 Created
Date: Sat, 24 Oct 2015 23:26:01 GMT
Server: Apache/2.4.10 (Unix) OpenSSL/1.0.1j PHP/5.6.3 mod_perl/2.0.8-dev Perl/v5.16.3
X-Powered-By: PHP/5.6.3
Location: api.domain.com/rest/v1/users/12345
Connection: close
Content-Language: en
Vary: User-Agent
Content-Type: application/json; charset=utf-8
Why doesn’t the JSON response return compressed? I purposely return >700kb just to make sure it compresses something.
I explicitly set the content-type header in my server before returning a JSON response. Is this not enough? I’m guessing the fact that it’s a PHP file may be the problem but I’m not experienced with Apache enough to know if it would be a problem/the fix.
However, if I add ob_start("ob_gzhandler”); to my PHP code, the content is compressed.
Also, it’s worth mentioning the .txt and index.php are in the same folder.
Try next (be sure filter_module is installed):
<IfModule filter_module>
FilterProvider COMPRESS DEFLATE "%{Content_Type} = 'application/json'"
</IfModule>

PHP Caching Files (.htaccess)

I have the following code which I am using for my website. I would like to know if this code is correct in order for me to effectively cache me pages and files. I have tried to use tools to check this but some say they don't see that I am caching.
<ifModule mod_headers.c>
# 1 Month
<filesMatch ".(ico|gif|jpg|jpeg|png|pdf)$">
Header set Cache-Control "max-age=2419200"
</FilesMatch>
# 1 Week
<filesMatch ".(css|js)$">
Header set Cache-Control "max-age=604800"
</FilesMatch>
# 1 Day
<filesMatch ".(htm|html)$">
Header set Cache-Control "max-age=86400"
</FilesMatch>
</ifModule>
Catching is the automatic mechanism of the browsers. When a page is loading, browser checks the cache for static files like js, css, images..., if they are not available browser will pus them into cache.
To determine whether the file is cached or not, check the firebug console and clik on request link.
here you can see information like
Accept-Ranges bytes
Cache-Control max-age=290304000, public
Content-Encoding gzip
Content-Length 2824
Content-Type application/javascript
Date Thu, 11 Jul 2013 10:15:06 GMT
Expires Fri, 12 Jul 2013 10:15:06 GMT
Last-Modified Thu, 03 Jan 2013 16:05:54 GMT
Server Apache
Vary Accept-Encoding,User-Agent

apache caching js files

I've configured Apache under windows to enable cache like this
LoadModule expires_module modules/mod_expires.so
LoadModule headers_module modules/mod_headers.so
LoadModule deflate_module modules/mod_deflate.so
ExpiresActive On
ExpiresDefault "access plus 3 days"
ExpiresByType image/x-icon "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType application/pdf "access plus 1 month"
<IfModule mod_headers.c>
# YEAR
<FilesMatch "\.(ico|gif|jpg|jpeg|png|flv|pdf)$">
Header set Cache-Control "max-age=29030400"
</FilesMatch>
# WEEK
<FilesMatch "\.(js|css|swf)$">
Header set Cache-Control "max-age=604800"
</FilesMatch>
# 45 MIN
<FilesMatch "\.(html|htm|txt)$">
Header set Cache-Control "max-age=2700"
</FilesMatch>
</IfModule>
When I try to open js file directly in browser (to test) by refreshing again and again then each time I get response status 200 with Firebug. Its not sending status "304 Not Modified". Have u any idea how can I achieve this. Following is the output of Firebug headers.
Response Header
Date Sat, 13 Aug 2011 01:18:15 GMT
Server Apache/2.2.19 (Win32) PHP/5.3.6
Last-Modified Sat, 13 Aug 2011 01:18:15 GMT
Etag W/"100000000171d-34f08-4aa5f022d9780"
Accept-Ranges bytes
Content-Length 216840
Cache-Control max-age=604800
Expires Tue, 16 Aug 2011 01:18:15 GMT
Keep-Alive timeout=5, max=100
Connection Keep-Alive
Content-Type application/javascript
Request Header
Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language en-us,en;q=0.5
Accept-Encoding gzip, deflate
Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection keep-alive
Cookie PHPSESSID=5k947khv1j27itd7mtp5evtg07
If-Modified-Since Sat, 13 Aug 2011 01:03:42 GMT
If-None-Match W/"100000000171d-34f08-4aa5f022d9780"
Cache-Control max-age=0
The request contains a if-modified-since-header:
Sat, 13 Aug 2011 01:03:42 GMT
But the response contains a last-modified-header with a time later than that:
Sat, 13 Aug 2011 01:18:15 GMT
..so the file has to be loaded again.
Try unsetting the last-modified-header: http://www.askapache.com/htaccess/apache-speed-last-modified.html

PHP session_start() overwrites HTTP Expires header

I am trying to set Expire header to 2 hours from access for text/html by using mod_expires like that:
<IfModule mod_expires.c>
ExpiresActive on
ExpiresDefault "access plus 2 hours"
ExpiresByType text/html "access plus 2 hours"
</IfModule>
However when used with PHP:
session_start();
Expires header is being reset to:
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Any ideas how to avoid that overwrite by session_start()?
OK, looks like have found an answer:
session_cache_limiter('public');
session_start();
does the trick, thanks.

Categories