.htaccess avoid only one file from browser catch - apache

I have below in my htaccess file.
ExpiresActive on
ExpiresDefault A0
ExpiresByType image/gif A29030400
ExpiresByType image/jpeg A29030400
ExpiresByType image/png A29030400
ExpiresByType text/css A29030400
ExpiresByType text/javascript A29030400
ExpiresByType application/x-javascript A29030400
Everything works perfectly fine. Now i have one config file which is config.js which i dont want browser to catch.
Does anybody have idea how i can avoid catching only one file using htaccess.
I have blow situation i am using filesmatch..if file is lang.js/config.js then set expiry to 1 day otherwise whatever specified in above.. is below setting correct or something else i need to do.
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType text/css "access plus 6 months"
ExpiresByType text/x-javascript "access plus 6 months"
ExpiresByType text/javascript "access plus 6 months"
ExpiresByType application/javascript "access plus 6 months"
ExpiresByType image/jpg "access plus 2 years"
ExpiresByType image/jpeg "access plus 2 years"
ExpiresByType image/gif "access plus 2 years"
ExpiresByType image/png "access plus 2 years"
ExpiresByType image/x-icon "access plus 2 years"
# Do not cache dynamically generated pages.
ExpiresByType text/html A1
# Cache all files for a month after access (A).
ExpiresDefault "access plus 21 days"
# Dont catch below files
<FilesMatch "^(lang|config)\.js$">
ExpiresDefault "access plus 1 day"
</FilesMatch>
</IfModule>
thank you in advance.
Regards,
Mona

Use the files directive
<Files "config.js">
#your desired config here
</Files>
For directory matching you can also use Directory directive
<Directory "/dir/with/different/config">
#your desired config here
</Directory>
EDIT AFTER QUESTION EDIT:
ExpiresDefault is used if there is not ExpiresByType for that content type. Assuming that config.js and lang.js are sent as application/javascript, you need to the change the last lines like this, to overwrite settings from the previous lines
<FilesMatch "^(lang|config)\.js$">
ExpiresByType application/javascript "access plus 1 day"
</FilesMatch>

Related

Apache Not Cacheing Images

# EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType application/javascript "access 1 month"
ExpiresByType application/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 2 days"
</IfModule>
## EXPIRES CACHING ##
I am using the above code in my .htaccess, but Page Speed Insights, GtMetrix etc are still saying my images not being cached. I tried various different examples posted and nothing is working. My host saids the issue is outside their scope... Any tips or direction to go in to try and diagnose the issue?

How to set Browser caching and expires headers via .htacess

Ive got a centOS lamp server running a static html, css, js site. Google's page speed tool suggested i "Leverage browser caching" (as this may be a google seo ranking factor we want to implement it). Is this something that can be done via the .htaccess file of my site ?
Ive set this up in the past but only via a wordpress plugin so it was pretty much one click.
Try to add this to the top of your htaccess-file:
## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType text/html "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 1 month"
</IfModule>
## EXPIRES CACHING ##

Location of .htaccess and httpd.conf files

I am trying to use leverage browser caching option in order to increase google page speed rank. For that purpose i need to add following code to .htaccess file or in httpd.conf file on apache server:
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 1 month"
ExpiresByType image/x-icon "access plus 1 year”
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType text/css "access 1 month”
ExpiresByType application/javascript "access plus 1 year"
</IfModule>
But i dont know where to find .htaccess and httpd.conf file. Can anybody tell where i will find these files?
.htaccess files are in any web directory, and you may need to enable visibility of dot-files to read them (.htaccess is considered a dot-file since it begins with a .).
To find the location of httpd.conf, use this link:
DistrosDefaultLayout - Httpd Wiki

How to control file caching using apache's .htaccess

I have below block in my htaccess file. Now i realized that there are two config files which i dont want to catch by browsers but other can be cached.
files are lang.ja and config.js so i thought of using below block
# Dont catch below files
<FilesMatch "^(lang|config)\.js$">
ExpiresDefault A0
</FilesMatch>
does any body have any idea how i should add above code in below existing block. i already tried adding above block at starting and ending of below block but it does not work. (it always set js to 6 months expiry.)
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType text/css "access plus 6 months"
ExpiresByType text/x-javascript "access plus 6 months"
ExpiresByType text/javascript "access plus 6 months"
ExpiresByType application/javascript "access plus 6 months"
ExpiresByType image/jpg "access plus 2 years"
ExpiresByType image/jpeg "access plus 2 years"
ExpiresByType image/gif "access plus 2 years"
ExpiresByType image/png "access plus 2 years"
ExpiresByType image/x-icon "access plus 2 years"
# Do not cache dynamically generated pages.
ExpiresByType text/html A1
# Cache all files for a month after access (A).
ExpiresDefault "access plus 21 days"
</IfModule>
Thank you in advance.
Regards,
Mona
Try FilesMatch directive without ^ and $:
<FilesMatch "(lang|config)\.js">
ExpiresDefault A0
</FilesMatch>
Place above code at the end of your .htaccess
Clear your browser cache or try in a new browser to test this out

Specifing Expiration Date For Static File's Caches

When i test my website for SpeedTest i'm seeing a lot of expiration not specified error.You can see at this page .
I added this code to my .htaccess file
## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 2 days"
</IfModule>
## EXPIRES CACHING ##
But nothing changed . Can you give an advice me, for can i specify expiration date for my static files ?
Server : Linux - Apache
i like the html5boilerplate way :
<IfModule mod_expires.c>
ExpiresActive on
# Perhaps better to whitelist expires rules? Perhaps.
ExpiresDefault "access plus 1 month"
# cache.appcache needs re-requests in FF 3.6 (thx Remy ~Introducing HTML5)
ExpiresByType text/cache-manifest "access plus 0 seconds"
# your document html
ExpiresByType text/html "access plus 0 seconds"
# data
ExpiresByType text/xml "access plus 0 seconds"
ExpiresByType application/xml "access plus 0 seconds"
ExpiresByType application/json "access plus 0 seconds"
# rss feed
ExpiresByType application/rss+xml "access plus 1 hour"
# favicon (cannot be renamed)
ExpiresByType image/x-icon "access plus 1 week"
# media: images, video, audio
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType video/ogg "access plus 1 month"
ExpiresByType audio/ogg "access plus 1 month"
ExpiresByType video/mp4 "access plus 1 month"
ExpiresByType video/webm "access plus 1 month"
# htc files (css3pie)
ExpiresByType text/x-component "access plus 1 month"
# webfonts
ExpiresByType font/truetype "access plus 1 month"
ExpiresByType font/opentype "access plus 1 month"
ExpiresByType application/x-font-woff "access plus 1 month"
ExpiresByType image/svg+xml "access plus 1 month"
ExpiresByType application/vnd.ms-fontobject "access plus 1 month"
# css and javascript
ExpiresByType text/css "access plus 2 months"
ExpiresByType application/javascript "access plus 2 months"
ExpiresByType text/javascript "access plus 2 months"
<IfModule mod_headers.c>
Header append Cache-Control "public"
</IfModule>
</IfModule>
Hope this can be useful to you.
Source:
https://github.com/h5bp/html5-boilerplate/blob/master/dist/.htaccess
You can use mod_headers to do the trick:
<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
Header set Cache-Control "max-age=290304000, public"
</FilesMatch>
Or you can use mod_expires:
ExpiresByType text/html "access plus 1 month 15 days 2 hours"
ExpiresByType image/gif "modification plus 5 hours 3 minutes"
I see you have PHP installed (reference wordpress), so we'll try to diagnose your problem:
restart your apache web server. If you own the apache server (command line access), you can do it like such.
create a phpinfo page. This will allow you to view the apache modules installed. Check for the mod_expires.c module your looking to use (should be able to do a CTL-F (find) request in your browser for mod_expires). If you find it - GOOD! If not, you need to install mod_expires in your apache server. If you own your apache server, you can search on for how to install modules for your specific operating system. If not, you can check with your hosting provider on how to get this module installed. Once installed, proceed.
modify your http.conf entry to include "plus" in your expression
restart Apache again
Test =)
You just need to add:
ExpiresByType application/javascript "access plus 1 year"
Maybe you didn't load mod_expires, so the entire stanza is a no-op. Or, AllowOverride is None and your .htaccess file is not processed.