Htaccess Cache Specifics files

Using Htaccess file you can speed up the loading time of your site with Header and ETag.

Example

Header unset ETag
FileETag None

# 21 DEC 2012
<filesMatch "(?i)^.*\.(ico|flv|jpg|jpeg|png|gif|js|css)$">
Header unset Last-Modified
Header set Expires "Fri, 21 Dec 2012 00:00:00 GMT"
Header set Cache-Control "public, no-transform"
</filesMatch>

# 3 HOUR
<filesMatch "\.(txt|xml|js|css)$">
Header set Cache-Control "max-age=10800"
</filesMatch>
 
# NEVER CACHE
<filesMatch "\.(html|htm|php|cgi|pl)$">
Header set Cache-Control "max-age=0, private, no-store, no-cache, must-revalidate"
</filesMatch>

htaccess how to redirect to www or not

An important thing to know with the .htaccess file is how to have a site with always the www prefix (http://www.mydomain.com) or without (http://mydomain.com).
To do that you need to use a 301 redirect, here there are the two examples:

from http://mydomain.com to http://www.mydomain.com

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www.mydomain.com$ [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [L,R=301]

from http://www.mydomain.com to http://mydomain.com

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^mydomain.com$ [NC]
RewriteRule ^(.*)$ http://mydomain.com/$1 [L,R=301]

Random posts