What is .htaccess ?
An .htaccess is a file controls how Apache interacts with website. When an .htaccess file is placed in domain’s directory, the .htaccess file is detected and executed by Apache and use or overrides the apache settings for the single website.With .htaccess files you can create redirects, rewrite URLs on your website, create password protected directories and others.
How enable .htaccess in Website?
Simply you can add the file with full name “.htaccess” on the root of website to configure the domain itself and use the Apache configuration especially for this domain.then you can start add the special configuration code on that file.
.htaccess to Expires and Cache-Control headers
What is Cache-Control is a HTTP header:Cache-Control is a HTTP header that defines the amount of time and manner a file is to be cached.
In order to add browser caching to website, you will need to set the date for when the cache expires. This date is of course flexible, and the optimal and responsible cache time for your site can differ depending on the type of website you are running. the following example different ways to set the expiration periods by years,months and seconds.
<IfModule mod_expires.c> # Enable Expirations Mode ExpiresActive On # Website Favicon Icon ExpiresByType image/x-icon "access plus 1 year" # Images Types 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 image/svg+xml "access plus 1 month" ExpiresByType text/x-javascript "access 1 month" # Support WebSite Types With Second Expiration Option ExpiresByType text/css "access plus 604800 seconds" ExpiresByType text/javascript "access plus 216000 seconds" ExpiresByType application/javascript "access plus 216000 seconds" ExpiresByType application/x-javascript "access plus 216000 seconds" ExpiresByType text/html "access plus 600 seconds" ExpiresByType application/xhtml+xml "access plus 600 seconds" </IfModule>how Cache-Control is a HTTP header works
When a new visitor browse website all the files like scripts, CSS styles and images are downloaded to the client temporary cache browser software. But when the user returns to same website it will reload the files from the cache rather than downloading them all again and this is one of the efficient configuration that my speed the web site .
.htaccess for Browser cache-control
Browser cache control is directly related with Cache-Control is a HTTP header and according to the Apache documentation provides directives to control and modify HTTP request and response headers. Headers can be merged, replaced or removed. This is an example on how to add this feature code inside htaccess file :
<ifModule mod_headers.c> <filesMatch "\.(pg|jpeg|png|gif|js|ico|swf)$"> Header set Cache-Control "public" </filesMatch> <filesMatch "\.(css)$"> Header set Cache-Control "public" </filesMatch> <filesMatch "\.(js)$"> Header set Cache-Control "private" </filesMatch> <filesMatch "\.(x?html?|php)$"> Header set Cache-Control "private, must-revalidate" </filesMatch> </ifModule>.htaccess for Gzip compression
To Optimize Website With GZIP Compression , we need to understand how Optimize Your Site With GZIP Compression do. Website GZIP compression makes it possible to reduce the file size of a web file (like HTML, PHP, CSS and Javascript files) and reduce bandwidth between 50-70% less of its original size before these files get sent to the browser of a user.
You can use one of two Apache mods to enable HTTP gzip compression,mod_gzip and mod_deflate .
Mod_gzip enables gzip compression and mod_deflate makes it possible to compress the output from your server before it is being sent to your visitor.and actually both mods are the same , so it doesn’t matter if you use either Mod_gzip or enables gzip.
To Use Apache Mod_gzip :
<ifModule mod_gzip.c> mod_gzip_on Yes mod_gzip_dechunk Yes mod_gzip_item_include file .(html?|txt|css|js|php|pl)$ mod_gzip_item_include handler ^cgi-script$ mod_gzip_item_include mime ^text/.* mod_gzip_item_include mime ^application/x-javascript.* mod_gzip_item_exclude mime ^image/.* mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.* </ifModule>
Using Apache mod_deflate:
<IfModule mod_deflate.c> AddOutputFilterByType DEFLATE text/html AddOutputFilterByType DEFLATE text/css AddOutputFilterByType DEFLATE text/javascript AddOutputFilterByType DEFLATE text/xml AddOutputFilterByType DEFLATE text/plain AddOutputFilterByType DEFLATE image/x-icon AddOutputFilterByType DEFLATE image/svg+xml AddOutputFilterByType DEFLATE application/rss+xml </IfModule>