Monday, February 7, 2011

What can I do with a .htaccess file? part2

Example 4: Force www prefix

Forcing your web site to always be accessible at www.yourdomain.com instead of yourdomain.com can be accomplished with mod_rewrite as well.
1RewriteEngine On
2RewriteCond %{HTTP_HOST} ^yourdomain.com
3RewriteRule ^(.*) http://www.yourdomain.com [R=permanent,L]

IP Restrictions

If you wish to limit who may visit a specific directory of your web site, placing a .htaccess file with the following code will either allow or deny them.  This feature is used when you wish to secure an area of your web site based on the IP address of the visitor.  If someone who has been denied access visits this page, they will receive a 403 error code response page which you may customize using the ErrorDocument directive.

Example 1: Allow only 1 IP

This will only allow a visitor with the IP address of 127.0.0.1.
1Order allow,deny
2Allow from 127.0.0.1
3Deny from all

Example 2: Disallow 1 IP

This will only disallow a visitor with the IP address of 127.0.0.1.
1Order deny,allow
2Deny from 127.0.0.1
3Allow from all

Example 3: Allow multiple IPs

This will only allow a visitor who’s IP address is either 192.168.0.1 or 192.168.0.2.  You may add as many IPs as you wish, adding each one on a new line.
1Order allow,deny
2Allow from 192.168.0.1
3Allow from 192.168.0.2
4Deny from all

Example 4: Allow IP range

This will only allow a visitor with an IP that begins with 192.168.0.* where * is any number between 0 and 255.
1Order allow,deny
2Allow from 192.168.0.*
3Deny from all

Directory Browsing

When you wish to display the contents of a directory similar to Windows Explorer or Mac’s Finder application, you should enable the option of Directory Browsing.  This will display a list of files in the directory that you place the .htaccess containing this code.

Example 1: Enable directory browsing

1Options +Indexes +MultiViews

Example 2: Hide file types

This will enable directory browsing but will hide all .php and .html files.
1Options  +Indexes +MultiViews
2IndexIgnore *.php *.html

Password Protected Directories

While Netfirms provides a method of password protecting directories via the Netfirms Control Panel, you may run out of available slots.  If this happens, you must manually password protect your directories using a .htaccess file.  There are two steps to creating a password protected directory or folder that you must complete.  First is the creation of the .htaccess file as well as the creation of the .htpasswd file.