Beginners guide to .htaccess file with examples – Part II

I hope you have gone through the examples listed in first part of this tutorial. If not, it’s here – [Beginners guide to .htaccess file with examples].

In this part, we will cover more examples with .htaccess file.

Presenting custom error pages

Use .htaccess file to present users with your custom pages for 401 [Authorization Required], 403 [Forbidden], 404 [not found] and 500 [Internal Server Error].

Syntax:
ErrorDocument < error-code > < location -of-custom-page>

Examples:
ErrorDocument 401 /401.html
ErrorDocument 403 /403.html
ErrorDocument 404 /404.html
ErrorDocument 500 /500.html

You can include some script in your customized pages to automatically send an email to you whenever those pages are called for. This way you will be notified every time a user encounters 404, 500 and other error messages.

Redirecting your-domain.com[non-www] to www.your-domain.com

From a search engine’s optimization point of view, we will use permanent redirection such that every time a request for non-www domain is made, it’s redirected and returns a status code of 301 [Permanent Redirect].

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

In the above lines, the first line tells the RewriteEngine to be On, second line mentions the Rewrite Condition and the last one is the rewrite rule.

Limiting Number of simultaneous connections

To limit the number of simultaneous connections to a directory or your entire site, use the below line. If you place it in a directory other than the root directory, then it will limit the connections to that directory and its sub-directories only. Placing it in htaccess file of root directory will implement it for entire site.

Syntax:
MaxClients < number-of-connections>

Examples:
MaxClients 40
MaxClients 100

Allow/Disallow certain visitors from accessing your site

To accomplish it use the following lines. Look at the syntax first:

Syntax:
Order allow,deny
Deny from < incoming -address >
Allow from < incoming -address>

The first line [Order allow,deny] tells what should be done first. The second line tells about denying incoming-addresses [could be a single IP, an IP block, domain name and all] and third line tells about the incoming-addresses [could be a single IP, an IP block, domain name and all] those should be allowed. If second line has ‘Deny all’, then you should change the order of allow,deny in the first line to deny,allow.

To deny access to a single IP address and allow everyone else
Order allow,deny
Deny from 100.100.100.1
Allow from all

To deny a block of IP address and allow everyone else. [Notice the second line]
Order allow,deny
Deny from 100.100.100.
Allow from all

To deny a single IP address and allow everyone else. [Use it to block referrals from a specific domain]
Order allow,deny
Deny from www.my-domain.com
Allow from all

To deny everyone else but yourself. [Notice the order of allow,deny has changed in first line and also appending more ‘Allow from’ lines at the end, which is valid]

Order deny,allow
Deny from all
Allow from < your-ip-address >
Allow from < another-ip-address >
Allow from < www.domain.com>

Specify Administrator’s email address in error message

Using the line below in your htaccess file will display the conerned system administrator’s email address in the error messages. If you are not using custom pages for error messages, then this will help your visitors drop an email and let you know about problems.

Syntax:
ServerAdmin < a-valid-email-address>

Example:
ServerAdmin [email protected]

I hope, that this tutorial will help you accomplish various tasks with ease. Your feedback, comments are welcome.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.