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 pageof 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]
[pic] 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.
[pic] 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 editingyour .htaccess file. [pic]
1 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. Let’s now move on to some common .htaccess file example.
2 Allow/Deny Directory Browsing
When directory browsing is on, people accessing a URL from your site with no index page or no pages at all, will see a list of files andfolders. To prevent such directory access, just place the following line in your .htaccess file.
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.
3 Redirect visitors from one page or directory to another
It’s quitesimple. 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]. You can also setup multiple redirects using htaccess.
Syntax: Redirect permanent [old directory/file name][space][new directory/file name]
Redirectpermanent /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.
4 Change the default index page of a directory or site
Almostevery 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...