.htaccess What, Why, When and How

March 17th, 2009

What

.htaccess is a file with a power to override certain configurations in the Apache httpd.conf file. If you’re using shared hosting, then you don’t have access to Apache main configuration. So most of shared hosting will provide you with .htaccess file.

Definition from Wikipedia :

.htaccess (hypertext access) is the default name of directory-level configuration files that allow for decentralized management of configuration when placed inside the web tree.

Why

You have your site up and running, but then again you ask yourself, why you need to configure and tweak your .htaccess.

If you think your site can run without dealing with .htaccess, then you absolutely right. But to optimize your website in term of bandwidth or rewrite your url to look nice in front of search engine, you have to know .htaccess.

When

You should know when you need to use .htaccess. There are disadvantages when using .htaccess.

  • Performance loss – There are additional file-system accesses for each HTTP request.
  • Security – Allowing each users to change the configuration of a server can cause security concerns.

However, you don’t have any choice if you’re using shared hosting. In shared hosting, you don’t have access to apache main configuration, so you better stick with .htaccess.

How

Here several things .htaccess file can do for you

  1. Enable Compression

    Compress your html pages to save bandwidth.

    AddOutputFilterByType DEFLATE text/html text/plain text/xml

    And you can also compress your javascript and css files.

    <IfModule mod_deflate.c>
    <FilesMatch "\.(js|css)$">
    SetOutputFilter DEFLATE
    </FilesMatch>
    </IfModule>

    Note, don’t compress your images. As image file format already in compression format.

  2. User Access Control via IP

    Deny access from all (not a good idea)

    #deny all access
    deny from all

    If you’d like to allow access from one specific IP

    #deny all access
    deny from all
    allow from 10.0.0.1

    or from a specific IP range

    allow from 192.168.0.0/24

    you can also block a specific file from access

    <Files komunitasweb.html>
    Order allow,deny
    Deny from all
    </Files>
  3. URL Redirect

    Redirect access to new URL.

    Redirect somedirectory/file.html http://komunitasweb.com/newfile.html
  4. Directory Listing

    Prevent listing all files.

    IndexIgnore *

    Prevent image files listing

    IndexIgnore *.gif *.jpg *.png
  5. Redirect to non-www

    Having both www and non-www is bad practice for Search Engine Optimization. Search engine will think you have duplicate content.

    <IfModule mod_rewrite.c>
    Options +FollowSymlinks
    RewriteEngine on
    RewriteCond %{http_host} ^www\.komunitasweb\.com[nc]
    RewriteRule ^(.*)$ http://komunitasweb.com/$1 [r=301,nc]
    </IfModule>
  6. Customized error page

    You can customize error pages, such as 404, 500 and others.

    ErrorDocument 404 /errors/404error.html
  7. Hide scripting language extension

    Set your html files to be processed by php, so your visitors doesn’t know script language you’re using.

    AddType application/x-httpd-php .html
  8. More

No related posts.

4 Comments