Manipulating HTTP Headers with htaccess
« htaccess Tricks for WebmastersPHP htaccess tips and tricks »
Apache .htaccess and httpd.conf have the power to send and manipulate HTTP Header Requests and responses like sending P3P privacy headers, Content-Type: UTF-8, Content-Language: en-US, etc. The power is immense and you can do some really cool stuff with HTTP Headers!
Check out my advanced HTTP Header Viewer and Manipulator – Free Online Tool.
« For Webmasters | .htaccess Tutorial Index | » PHP htaccess tips
Any time you see a meta tag of type “http-equiv” you can replace it with a real header in htaccess
This is similar to how google ads employ the header Cache-Control: private, x-gzip-ok="" to prevent caching of ads by proxies and clients.
<FilesMatch "\.(html|htm|js|css)$"> FileETag None <IfModule mod_headers.c> Header unset ETag Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate" Header set Pragma "no-cache" Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT" </IfModule> </FilesMatch>
<FilesMatch "\.(html|htm)$"> <IfModule mod_headers.c> Header set imagetoolbar "no" </IfModule> </FilesMatch>
Adding a P3P header to your site is a good idea, do this.
<IfModule mod_headers.c> Header set P3P "policyref=\"/w3c/p3p.xml\", CP=\"NOI DSP COR NID CUR ADM DEV OUR BUS\"" # OR THIS, SIMPLER Header set P3P "policyref=\"/w3c/p3p.xml\"" </IfModule>
Article: Setting Charset in htaccess
AddDefaultCharset UTF-8 AddLanguage en-US .html .htm .css .js
AddType 'text/html; charset=UTF-8' .html
Article: Using ‘Files’ in htaccess
<Files ~ "\.(htm|html|css|js)$"> AddDefaultCharset UTF-8 DefaultLanguage en-US </Files>
Article: Using ‘FilesMatch’ in htaccess
<FilesMatch "\.(htm|html|css|js)$"> AddDefaultCharset UTF-8 DefaultLanguage en-US </FilesMatch>
« For Webmasters | .htaccess Tutorial Index | » PHP htaccess tips
« htaccess Tricks for Webmasters
PHP htaccess tips and tricks »
Please consider donating to support active development of the free software and articles here.![]()
The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect. Tim Berners-Lee
Will the Prevent Caching 100% work for executables? I have an EXE on our site that I use for remote support. I frequently update it, but I want to make sure that that my end users always download the current version, and not one in their cache.
Tylan
I surf the net for an easy to understand guide on htaccess.
Finally i have a deeper idea on htaccess with your guide.
Intelligent work. Thanks.
Thank you for this amazing wealth of knowledge! It’s more definitive (and easier to understand) than the main apache and php.net forums.
QUESTION:
You suggest “prevent file caching” to speed up a site. Doesn’t turning OFF caching slow down the site? Seems it’s faster to pull a file from cache than request it …unless I’m missing or confusing this with something else?
Thanks again! -Mike
Just a note for “100% Prevent Files from being cached“:
You need to enable mod_headers otherwise it drops
500 Internal Server Error
I’m just wondering about this P3P. I read about it, and was wondering if any browser implements this?
I have multiple folders inside an assets folder that contain mp3 & swf files that I do not want to cache. this is my htaccess code not sure if im using it correctly
RewriteEngine on
rewritecond %{http_host} ^ninjatactics.net [nc]
rewriterule ^(.*)$ http://www.ninjatactics.net/$1 [r=301,nc]
FileETag None
Header unset ETag
Header set Cache-Control "max-age=0, no-cache, no-store, private"
Header set Pragma "no-cache"
Header set Expires "0"
i am using the “100% Prevent Files from being cached” htaccess code but not sure i am using it correctly. I created the file in placed it in the root directory. do i need to specify where my files are locate in the htaccess file?
imagetoolbar = no
doesn’t works, you have to specify the “http-equiv” explicitly. Why that?
Does ‘100% Prevent Files from being cached’ also works for websites displaying ads in an php environment instead of html?
It's very simple - you read the protocol and write the code. -Bill Joy
HTML | DCMI | GRDDL | XOXO | XDMP | XFN | DOM | XML | XHTML 1.1 Strict | CSS 2.1 | W3C | TLDP | WAI | DISA | ICSI | GIAC | SANS RR | GHOST | DEFCON | NIST | DHS CYBER | NIST | .:: Phrack Magazine ::.
↑ TOPExcept where otherwise noted, content on this site is licensed under a Creative Commons Attribution 3.0 License, just credit with a link.
This site is not supported or endorsed by The Apache Software Foundation (ASF). All software and documentation produced by The ASF is licensed. "Apache" is a trademark of The ASF. HTTPD based on NCSA HTTPd
Does this work for EXE files as well?