« htaccess Tricks for WebmastersPHP htaccess tips and tricks »
Manipulating HTTP Headers with htaccess
April 10th, 2007
Contents
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
Custom HTTP Headers
Any time you see a meta tag of type "http-equiv" you can replace it with a real header in htaccess
- Prevent Caching 100%
- Remove IE imagetoolbar without meta tag
- Add Privacy (P3P) Header to your site
- Add a 'en-US' language header and 'UTF-8' without meta tags!
100% Prevent Files from being cached
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>
Remove IE imagetoolbar
<filesMatch "\.(html|htm)$"> <ifModule mod_headers.c> Header set imagetoolbar "no" </ifModule> </filesMatch>
Add P3P Privacy Headers to your site
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>
Add a "en-US" language header and "UTF-8" without meta tags!
Article: Setting Charset in htaccess
AddDefaultCharset UTF-8 AddLanguage en-US .html .htm .css .js
Using AddType
AddType 'text/html; charset=UTF-8' .html
Using the Files Directive
Article: Using 'Files' in htaccess
<files ~ "\.(htm|html|css|js)$"> AddDefaultCharset UTF-8 DefaultLanguage en-US </files>
Using the FilesMatch Directive
Article: Using 'FilesMatch' in htaccess
<filesMatch "\.(htm|html|css|js)$"> AddDefaultCharset UTF-8 DefaultLanguage en-US </filesMatch>
htaccess Guide Sections
- htaccess tricks for Webmasters
- HTTP Header control with htaccess
- PHP on Apache tips and tricks
- SEO Redirects without mod_rewrite
- mod_rewrite examples, tips, and tricks
- HTTP Caching and Site Speedups
- Authentication on Apache
- htaccess Security Tricks and Tips
- SSL tips and examples
- Variable Fun (mod_env) Section
- .htaccess Security with MOD_SECURITY
- SetEnvIf and SetEnvIfNoCase Examples
« For Webmasters | .htaccess Tutorial Index | » PHP htaccess tips
Reader Comments
-
Do you know if there is a way to always set a 503 error code for a specific file? I'm working on a npi script which mustn't lost any request. But any php fatal error (when I'm updating the script for example) returns header 200. That should mean my script process the request, but it didn't because a fatal error. I know the caller repeat the request when get a 503 error, so I wonder to set 503 as the default response by htacess and send header 200 using the php function header (if that execute, so there was no fatal error). That make sense? Could you point me if there is a way to set header code by htacess?
-
Be aware that when you “prevent file caching”, you will get an impact on load speed, because every request thinks it has a "modified" status that will make a browser fetch the data from the server again, and again... and again. Cool thing if you want to see your bandwidth used and if you want to ensure users going bezerk looking at the "loading" status message... my 2 cents: CACHE all CSS, JS, JPG, PNG, GIF and any other static data. If you're using PHP and expect it to be fresh every time you load it, don't cache... but if you use a CMS... CACHE your PHP too! Don't confuse people with wrong or incomplete information... it downgrades the value of your writings.
-
I tried to follow your example of '100% Prevent Files from being cached' by adding the following lines to my
httpd.conffile:Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate" Header set Pragma "no-cache"
When I use theFilesMatchconditional statement, I don't receive the expected results:HTTP/1.1 200 OK Date: Mon, 07 Jun 2010 23:17:31 GMT Server: Apache/2.2.3 (Red Hat) Connection: close Transfer-Encoding: chunked Content-Type: text/html; charset=UTF-8
If I remove theFilesMatchand use the following:Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate" Header set Pragma "no-cache"
I get the expected results:HTTP/1.1 200 OK Date: Mon, 07 Jun 2010 23:15:11 GMT Server: Apache/2.2.3 (Red Hat) Cache-Control: max-age=0, no-cache, no-store, must-revalidate Pragma: no-cache Connection: close Transfer-Encoding: chunked Content-Type: text/html; charset=UTF-8
Can you see what I may be doing wrong with the 'FilesMatch' statement? -
I like to use this free http send header tool to manipulate and send HTTP request and view the response easily.
Pingback:5 Asset Management Tricks for Faster Websites | Jon Raasch's Blog
-
For some locations we can disable the caching system? For example : This is my .htaccess content, and I have to disable php file caching for some locations.
Header set Cache-Control "max-age=86400, public" Header set Expires "Thu, 8 May 2010 20:00:00 GMT" Header unset Last-Modified
I want to do thisHeader set Cache-Control "max-age=86400, public" Header set Expires "Thu, 8 May 2010 20:00:00 GMT" Header unset Last-Modified # Header set Cache-Control "max-age=0" # Header set Expires "Thu, 8 May 2000 20:00:00 GMT" # Header unset Last-Modified
How can I do that? Thanks. -
100% Prevent Files from being cached
works like a charm to stop caching files.... Thank you very much. -
100% working ... Blocking Cookies? :)
-
Does this work for EXE files as well?
-
Will the Prevent Caching 100% work for executables? I have an
EXEon 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 = nodoesn'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?
