Important Information about .htaccess file


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 and folders. To prevent such directory access, just place the following line in the .htaccess file.
IndexIgnore */*
Many hosting companies, by default deny directory browsing and having said that, just in case someone need to enable directory browsing, place the following line in the .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 the .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]
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 site or directory does not has a file name which matches a name from the list above, chances are that 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 I 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 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

Preventing hot linking of images from your website

If website contains images which people from other websites are linking to and I am getting extra 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 the actual domain name [without www], and instead of (www.\), use the actual subdomain name (sub-domain.\)

Prevent access to your .htaccess file (.htaccess security) 

To prevent visitors from viewing the .htaccess file, place the following lines in the file. Of course, by default most Apache installations will not show .htaccess file but just in case.
<Files .htaccess>
order allow,deny
deny from all
</Files>

Comments

Popular posts from this blog

Puzzle

Importing Java Library

Examples on mod_rewrite