Thursday, September 15, 2011

The .htaccess file - More than just redirects!


.htaccess is only a file in your home folder. But it can do wonders. It can change settings on the servers and allow you to do many different things. The .htaccess file isn’t difficult to use and is really just made up of a few simple instructions in a text file. Let me note down a few situations where the .htaccess file can be used. These are requests that I get frequently and hope this article will help you get it done, all by yourselves.
First and foremost, we need to see if .htaccess is enabled in our server. For those who have root access, check the Apache configuration file, and ensure that the following entry is set:
———————-

AllowOverride All

———————-
For those who have no root access, please check with your support team.
You can also change the name from .htaccess to anything else like .config. You just need to set the following in the Apache configuration file:
———-
AccessFileName .config
———-

Now lets get on with the various applications:

To set custom error pages

To point a certain error message to a custom file, put this in your .htaccess file:
———
ErrorDocument 404 http://www.yourdomainname.com/filename.html
———
Where 404 is the error message you are redirecting, and http://www.yourdomain.com/filename.html is the page you wish people to see when they receive the error.
URLs will begin with a slash (/) for local URLs, or will be a full URL which the client can resolve.
Examples:
ErrorDocument 500 /cgi-bin/tester
ErrorDocument 404 /cgi-bin/bad_urls.pl
ErrorDocument 401 http://www2.foo.bar/subscription_info.html
ErrorDocument 403 “Sorry can’t allow you access tod

Redirect a page using .htaccess

To redirect visitors to certain pages based on the directory or file they request, add this to your .htaccess file:
————-
Redirect /directory http://www.domain.com/new.html
————-
Where /directory is the URL of the directory or file that you wish to redirect, and http://www.domain.com/new.html is the URL you are redirecting to

Protecting a directory using .htaccess

If you want to set authentication, and prevent other users from entering certain area, here is the .htaccess code to
require passwords:
————–
AuthType Basic
AuthUserFile /home/user/.htpasswd
AuthGroupFile /dev/null
AuthName “Members Area”
require valid-user

————–

Deny users using .htacccess

Add the following to the .htaccess file:
————–

order allow,deny
deny from 128.23.45.
deny from 122.2.2.2
allow from all

————–
This is an example of a .htaccess file that will block access to your site to anyone who is coming from any IP address beginning with 128.23.45 and from the specific IP address 122.2.2.2 . By specifying only part of an IP address, and ending the partial IP address with a period, all sub-addresses coming from the specified IP address block will be blocked. You must use the IP addresses to block access. Use of domain names is not supported.

Redirect to a machine name

Add the following to the .htaccess file:
————–
RewriteEngine On
Options +FollowSymlinks
RewriteBase /
# Rewrite Rule for machine.domain-name.net
RewriteCond %{HTTP_HOST} machine.domain-name.net $
RewriteCond %{REQUEST_URI} !machine/
RewriteRule ^(.*)$ machine/$1

————–
This will redirect requests for the machine name machine.domain-name.net to the directory machine on the site
domain-name.net .

Prevent hot links: Preventing People from Linking to Your Images

Add the following to the .htaccess file:
————–
# Rewrite Rule for images
RewriteCond %{HTTP_REFERER}
RewriteRule ^(.*)$ http://
————–
You would replace the above with the domain name and path of the page that is referring to your domain. For example: www.their-isp.net/users/mypage/
The RewriteCond directive states that if the {HTTP_REFERER} matches the URL that follows, then use the RewriteRule directive. The RewriteRule directive will redirect any reference back to the referring web page.
——————————————————-
Reference:
http://help.mindspring.com/webhelp/resources/powertips/accessindex.htm
http://www.javascriptkit.com/howto/htaccess.shtml
http://apache-server.com/tutorials/ATusing-htaccess.html
http://www.webdeveloper.com/servers/servers_htaccess_magic.html
http://baremetal.com/gadgets/htaccess/
——————————————————-