Monday, November 27, 2006

Working with .htaccess files

.htaccess files provides us with ways to make configuration changes on a per-directory basis. This file works well in Apache Web Server and on Linux/Unix. Also, it works on Windows based system with Apache Web server.

There are several things that developers, site owners and webmasters can do by using .htaccess file. Let’s look at some of them:

  • Prevent directory browsing
  • Redirect visitors from one page or directory to another
  • Password protection for directories
  • Change the default index page of a directory
  • Prevent hot-linking of images from your website

Since .htaccess file allows us to make changes on a per-directory basis, the following are valid places to put the .htaccess file in:

/.htaccess [placing in root folder of the site]
/content/.htaccess [placing in content folder]
/content/html/images/.htaccess [in the images folder]

:idea: Any command that you place in .htaccess file will affect it’s current directory where it is placed and also it’s sub-directories. You may put a .htaccess file in the root folder such that it will affect the whole site.

:idea: Make a backup of your .htaccess file [if you have any] before you attempt any of the settings mentioned in this article. I must not be held responsible for any consequences that arises due to editing your .htaccess file. ;)

Working with .htaccess files

For creating and editing purpose, a normal text editor such as notepad will do. Alternatively, you can download a free copy of PSPad for easy editing. To be able to see files in your FTP software, you must enable settings in your FTP client to see hidden files on the remote server [applicable to your system as well]. When done editing, you can save the file with double quotes in windows. [Save file as “.htaccess”]. This will save the file as .htaccess and will not prompt you for a file name as such. I think you have quite understood these instructions. Let’s move on to some common examples and usages of .htaccess file.

Allow/Deny Directory browsing

With directory browsing on, people when open a URL from your site with no index page or no pages at all, will see all it’s files and folders. To prevent such directory viewing, just place the following line in your .htaccess file.

IndexIgnore */*

Many hosting companies, by default deny directory browsing and having said that, just in case you need to enable directory browsing, place the following line in your .htaccess file.

Options +Indexes

Redirect visitors from one page or directory to another

It’s quite simple. Look at the example lines below and place similar lines in your .htaccess file of the root folder and it will do the rest. [Remember to use permanent keyword in the line to tell the search engines that the old link has moved to the new link]
Syntax: Redirect permanent [old directory or file name][space][new directory or file name]

Redirect permanent /olddirectory /newdirectory
Redirect permanent /olddirectory /somedirectory/newdirectory
Redirect permanent /oldhtmlfile.htm /newhtmlfile.htm
Redirect permanent /oldhtmlfile.htm http://your-domain.com/newhtmlfile.htm

All the above lines are valid. Just remember to replace the file/directory names with actual ones.

Change the default index page of a directory or site

Almost every hosting company will have index.htm, index.html, index.php, index.asp, default.asp, default.html as the default index page names in their web server settings. So, in case your site or directory does not has a file name which matches a name from the list above, chances are that your visitors will either see a list of all the files and folders [through directory browsing] or will not see anything at all. To change the default index page’s name for a directory or the site, place the following line in the .htaccess file of the root folder or the particular directory for which you want to change the index page’s name.

DirectoryIndex homepage.htm
DirectoryIndex somepage.htm

To have more names, put a space between file names and it will take into considerations all those file names as possible index page names. Which means, if it finds a filename matching a list of names you supplied [in the given order] in .htaccess, then it will open that page as the index page for the directory. The below line, with multiple names, is also a valid usage:

DirectoryIndex homapage.html somepage.html myindexpage.html anything.html

:idea: Remember, each entry must be in one line only.

Prevening hot-linking of images from your website

If your website contains images which people from other websites are linking to and you get charged for the extra bandwidth, then placing the following lines will prevent any such image hot-linking. Most of the hosting companies provide this feature in their control panel itself, such as CPanel. This trick requires mod_rewrite engine to be on in Apache on your web server.

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www.)?your-domain.com/.*$ [NC]
RewriteRule .(gif|jpg)$ - [F]

In the above code, replace [your-domain] with your actual domain name [without www]

Prevent access to your .htaccess file

This article would have remained incomplete without mentioning this trick. ;) To prevent visitors from viewing your .htaccess file, place the following lines in your file.

<Files .htaccess>
order allow,deny
deny from all
</Files>

More information and detailed documentation, visit Apache website.

Get more information

Can't find what you're looking for? Try Google Search!
Google
 
Web eshwar123.blogspot.com

Comments on "Working with .htaccess files"

 

Anonymous Anonymous said ... (8:37 am) : 

All of the in the most apt way payday impart services we reviewed are suited, light institutions that nowadays a attest to utilization to those who ask for a too little extraneous dollars to exhort it through a debauched patch. In this instal, you'll find out articles with payday loans guidance and capital tips, as opulently as satiated reviews and a side nearby side conflict to plagiarize you oblige an briefed settling on which repair is amend toward your short-term conformation needs. We bring about that the most first-rate options allowing in behalf of on the subject of payday loans online.

Payment those that need pinch cash between paydays, arrangement the differences in payday loan lenders can determine how hands down and at you fall heir to the boodle you need. It in use accustomed to to be that you had to be cast to a physical fingers on and wait for an affirmation on your payday loan, after submitting copies of check out stubs and bank statements. Modern, there is a inconsistency in payday loan lenders because there are some that proffer express and within easy reach online options. When you receive benefit of online options, it is achievable to support twinkling of an eye approvals and get the shekels you fundamental in a topic of a few hours, or less.


Best Online Payday Loans and Cash Advance:
payday advance loan
[url=http://paydayloanmoneyfast.com/loan/no-faxing-cash-advances-3d]No faxing cash advances[/url]
http://paydayloanmoneyfast.com/loan/payday-loans-without-faxing-ba - Payday loans without faxing

 

Anonymous Anonymous said ... (11:04 am) : 

My partner and I absolutely love your blog and
find most of your post's to be just what I'm looking for.
Does one offer guest writers to write content for you personally?
I wouldn't mind writing a post or elaborating on a few of the subjects you write in relation to here. Again, awesome web site!

Here is my page :: additional info

 

Anonymous Anonymous said ... (8:42 am) : 

Excellent article. Keep posting such kind of info on your
blog. Im really impressed by it.
Hey there, You've performed an incredible job. I'll certainly digg it and in my view recommend to my
friends. I'm confident they'll be benefited from this website.


My web site :: Full Article

 

Anonymous Anonymous said ... (6:54 pm) : 

Keep on working, great job!

My homepage :: Chaussure De Foot Pas Cher