Basics of .htaccess

The .htac­cess file is a sim­ple ASCII file and is a con­fig­u­ra­tion file for use on web servers run­ning the Apache Web Serv­er software.

When a .htac­cess file is placed in a direc­to­ry, then the .htac­cess file is detect­ed and exe­cut­ed by the Apache Web Serv­er. These .htac­cess files can be used to alter the con­fig­u­ra­tion of the Apache Web Serv­er to enable/​disable addi­tion­al func­tion­al­i­ty and fea­tures that the Apache Web Serv­er has to offer.

.htac­cess files affect the direc­to­ry they are placed in and all sub-direc­to­ries, .htac­cess files must be uploaded as ASCII mode, not BINARY. You may need to CHMOD the .htac­cess file to 644 or (RW‑R–R—). Which makes it web serv­er read­able but not the brows­er readable.

Disabling Directory Browsing

When there are no default files to load like index.html or index.php etc., then the serv­er shows the direc­to­ry list­ing. If you want to dis­able it then

Options All -Indexes

If you want not to dis­play par­tic­u­lar type then like .wmv, .mp4 and .avi etc.,

IndexIgnore *.wmv *.mp4 *.avi

Error Documents

You can spec­i­fy the error doc­u­ment files for the errors like 403, 404 and 500 etc.,

ErrorDocument 403 /forbidden.html
ErrorDocument 404 /notfound.html
ErrorDocument 500 /servererror.html

Change Default Page

By default the apache loads index.html, index.php etc., index files. But if you want to change and give your own files like home.html default.html etc…

 DirectoryIndex home.html default.html index.html index.php

the apache tries to load them in order, first it tries load home.html if it is not present then tries to load default.html etc.,

Redirect Visitors to New Page/​Directory

You might have changed  a page to newone and you want to redi­rect all the view­ers com­ing to the old page to new page. And also works for folder/​directory.

Redirect oldpage.html http://www.domainname.com/newpage.html
Redirect /olddir http://www.domainname.com/newdir/

Block Hot Linking/​Bandwidth hogging

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^<a href="http://(www\.)?mydomain.com/.*">http://(www\.)?mydomain.com/.*</a>$ [NC]
RewriteRule \.(gif|jpg)$ - [F]

If you want to show a default image too?

Add this below the Hot Link Block­ing code:

RewriteRule \.(gif|jpg|png)$ <a href="http://www.mydomain.com/stealingisbad.gif">http://www.mydomain.com/stealingisbad.gif</a> [R,L]

Enable Gzip – Save Bandwidth

By Enabling Gzip the serv­er serves the spec­i­fied files in gZip com­pressed for­mat, It will save the bandwidth.

# Begin GZip Compression
<ifmodule mod_deflate.c>
AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript
</ifmodule>
# End GZip

Set an Expires header and enable Cache-Control

For SEO it is rec­om­mend­ed to set the Expire head­ers for all the types of files, It means after spec­i­fied peri­od time the brows­er looks for the updat­ed infor­ma­tion regard­ing that kind of file.

<ifmodule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 1 seconds"
ExpiresByType text/html "access plus 7200 seconds"
ExpiresByType image/gif "access plus 518400 seconds"
ExpiresByType image/jpeg "access plus 518400 seconds"
ExpiresByType image/png "access plus 518400 seconds"
ExpiresByType text/css "access plus 518400 seconds"
ExpiresByType text/javascript "access plus 216000 seconds"
ExpiresByType application/x-javascript "access plus 216000 seconds"
</ifmodule>

For the cache control

<ifmodule mod_headers.c>
# Cache specified files for 6 days
<filesmatch "\.(ico|flv|jpg|jpeg|png|gif|css|swf)$">
Header set Cache-Control "max-age=518400, public"
</filesmatch>
# Cache HTML files for a couple hours
<filesmatch "\.(html|htm)$">
Header set Cache-Control "max-age=7200, private, must-revalidate"
</filesmatch>
# Cache PDFs for a day
<filesmatch "\.(pdf)$">
Header set Cache-Control "max-age=86400, public"
</filesmatch>
# Cache Javascripts for 2.5 days
<filesmatch "\.(js)$">
Header set Cache-Control "max-age=216000, private"
</filesmatch>
</ifmodule>