Useful .htaccess Tricks For Web Developers and Designers

useful-.htaccess-tricks-for-web-developers-and-designers

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.

ErrorDocument 404 /404.html

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

IndexIgnore *
OR
Options All -Indexes

Change the Timezone of your server

You can set your server time zone

SetEnv TZ your-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_on Yes
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.

AddType application/octet-stream .avi .mpg .mov .pdf .xls .mp4

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

# year
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

RewriteEngine On
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:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.yourdomain.com [NC]
RewriteRule ^(.*)$ http://yourdomain.com/$1 [L,R=301]

Redirect non-www to www:

RewriteEngine On
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

Header set Set-Cookie “language=%{lang}e; path=/;” env=lang

Prevent Hotlinking

Preventing hotlinking can reduce bandwidth, by disallowing other websites from displaying images hosted on your server.

Options +FollowSymlinks
# 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.

order allow,deny
deny from “ip address”
allow from all

Block viewing of .htaccess

Below is the sample code to prevent viewing of .htaccess file:

order allow,deny
deny from all

cheers!!
Joginder Poswal

Tags: ,

This entry was posted on Monday, August 10th, 2009 at 11:26 pm and is filed under Others, Web Design, Web Standard. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

4 Responses to “Useful .htaccess Tricks For Web Developers and Designers”

  1. rajender Says:

    Very useful informations … i’ll use some of the information on my projects…:)

    Thanks
    Raj

  2. savita Says:

    This article was very nice and helpful to me, thanks.

  3. babylon 8 download Says:

    You post great articles. Bookmarked !

  4. jposwal Says:

    thanks

Leave a Reply

Anti-Spam Protection by WP-SpamFree

Get Adobe Flash playerPlugin by wpburn.com wordpress themes