Makiomar Makiomar - 5 months ago 53
PHP Question

can't cache static files using htaccess

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


<!doctype html>
<link rel="stylesheet" href="style.css">
<h1 id="heading">Test</h1>

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.

<IfModule mod_expires.c>

ExpiresActive on
ExpiresDefault "access plus 1 month"


ExpiresByType text/css "access plus 1 year"


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


I have found that mode_expires was not active on localhost and i enabled it but still facing the problem

Answer Source

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 "\.(js)$">
 Header set Cache-Control "max-age=2592000, private"
<filesMatch "\.(html|htm)$">
Header set Cache-Control "max-age=7200, public"
# Disable caching for scripts and other dynamic files
<FilesMatch "\.(pl|php|cgi|spl|scgi|fcgi)$">
Header unset Cache-Control
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download