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 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" </FilesMatch>
Remove IE imagetoolbar
<FilesMatch "\.(html|htm)$"> Header set imagetoolbar "no" </FilesMatch>
Add P3P Privacy Headers to your site
Adding a P3P header to your site is a good idea, do this.
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\""
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
Related Articles
11.08.07 at 11:42 am
Does ‘100% Prevent Files from being cached’ also works for websites displaying ads in an php environment instead of html?
04.02.08 at 5:42 am
imagetoolbar = nodoesn’t works, you have to specify the “http-equiv” explicitly. Why that?
04.07.08 at 7:49 pm
@ Daniel
Works for me.. the
http-equivmeta tag literally meanshttp header equivelant. http header is better of course.04.08.08 at 9:31 am
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?
04.08.08 at 11:41 am
@ johnny
use the http header viewer tool to see if its working, if not then paste your .htaccess
04.08.08 at 9:02 pm
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"05.03.08 at 12:22 am
[...] information to learn how to prevent the caching of particular filenames using an .htaccess file: http://www.askapache.com/htaccess/using-http-headers-with-htaccess.html#prevent-caching-with-htacces... Tags: browser caching, cache-control, htaccess, http headers, no-cache header Posted 2008-05-02 [...]