The .htaccess file is a simple ASCII file and is a configuration file for use on web servers running the Apache Web Server software.
When a .htaccess file is placed in a directory, then the .htaccess file is detected and executed by the Apache Web Server. These .htaccess files can be used to alter the configuration of the Apache Web Server to enable/disable additional functionality and features that the Apache Web Server has to offer.
.htaccess files affect the directory they are placed in and all sub-directories, .htaccess files must be uploaded as ASCII mode, not BINARY. You may need to CHMOD the .htaccess file to 644 or (RW‑R–R—). Which makes it web server readable but not the browser readable.
Disabling Directory Browsing
When there are no default files to load like index.html or index.php etc., then the server shows the directory listing. If you want to disable it then
Options All -Indexes
If you want not to display particular type then like .wmv, .mp4 and .avi etc.,
IndexIgnore *.wmv *.mp4 *.avi
Error Documents
You can specify the error document 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 redirect all the viewers coming 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 Blocking 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 server serves the specified files in gZip compressed format, 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 recommended to set the Expire headers for all the types of files, It means after specified period time the browser looks for the updated information regarding 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>