The .htaccess files (Hypertext Access file) is the default name of a configuration file for use on web servers running the Apache Web Server software. It contains commands known by the server that tell the server how to behave in certain instances. The .htaccess file is placed in your website root directory, and is able to override a subset of the server’s global configuration; the extent of this subset is defined by the web server administrator.
Most common uses of an htaccess file include the capacity to restrict access (password-protection) to specific files or directories on the Internet. It also nclude basic redirect functionality, by which we can specify a particular webpage to be accessed when there the file requested by the browser is not found (error 404).
Display a Custom 404 Error Page:
If You may want to redirect your users to an error page is any of the http errors like 404 occurs. It Can replace the server’s default error page with one of your own page or you can links visitors to your home page.
Prevent Directory Listing
When there’s no index page in a directory, visitors can look and see what’s inside. You can restrict it easily by following sample code
OR
Options All -Indexes
Change the Timezone of your server
You can set your server time zone
example
SetEnv TZ Asia/Calcutta
check out A list of timezones at: http://us2.php.net/manual/en/timezones.php
Compress output using GZIP
You can use htaccess gzip for Faster Loading and Bandwidth Saving. As it compress all the text file, css, js, html files with GZip
mod_gzip_dechunk Yes
mod_gzip_item_include file \.(html?|txt|css|js|php|pl|jpg|png|gif)$
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.*
Force Files Like PDF or DOC as “File Save As” Prompt
Sometimes you want files in certain format like PDF or DOC to be downloaded instead of opened in new browsers.
Browser Caching using .htaccess
The best way to increase your website loading time is to use a cache system or browser cache. Cache the static files or images or CSS file and improve your website performance
Header set Cache-Control “public”
Header set Expires “Thu, 15 Apr 2010 20:00:00 GMT”
Header unset Last-Modified
#2 hours
Header set Cache-Control “max-age=7200, must-revalidate”
SetOutputFilter DEFLATE
Header set Expires “Thu, 15 Apr 2010 20:00:00 GMT”
Redirect browser to https
Sometimes you will require an SSL connection. Add following in to your htaccess and redirect entire website to https
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
Redirect non-www to www URL’s or www to non-www (301 Redirect )
Some people prefer to use www.somesite.com, but some people prefer the shorter somesite.com. But everyone want that all of the visitors get sent to the same place. Its useful for Seo purpose also.
Redirect www to non-www:
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.yourdomain.com [NC]
RewriteRule ^(.*)$ http://yourdomain.com/$1 [L,R=301]
Redirect non-www to www:
RewriteBase /
RewriteCond %{HTTP_HOST} ^yourdomain.com [NC]
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [L,R=301]
Set Cookie using htaccess
You ca set Cookie with environment variable
Prevent Hotlinking
Preventing hotlinking can reduce bandwidth, by disallowing other websites from displaying images hosted on your server.
# no hot-linking
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?edrackham\.com/ [nc]
RewriteRule .*\.(gif|jpg|png)$ http://edrackham.com/img/hotlink_f_o.png [nc]
Deny/Allow IP (block IP)
Sometimes you may need prevent access your site from IP of allow only for specific IP address.
deny from “ip address”
allow from all
Block viewing of .htaccess
Below is the sample code to prevent viewing of .htaccess file:
deny from all
cheers!!
Joginder Poswal
Very useful informations … i’ll use some of the information on my projects…:)
Thanks
Raj
This article was very nice and helpful to me, thanks.
You post great articles. Bookmarked !
thanks