What is .htaccess
A .htaccess file is configuration file supported by several web servers. It is a file used for server configuration. When it is kept in a directory, which is loaded by a server then .htaccess file is executed by the apache server. This helps in basic redirecting facility, a 404 error not found and password protection and other hot links. It is also used to enable and disable the additional functionality and features that server offers.
You can make it hidden in a UNIX based system by using (.)Dot. It is always present in directive syntax but not written in conventional programming, its format is limited subset of the Apache HTTP server's configuration file httpd.conf even when used with web servers such as Zeus Web Server and Oracle iPlanet Web Server. These two servers have very different native global configuration files. Including other advantages like changes made immediate effect on every request and it only allows individual users ability to alter their site configuration. But this also leads to performance loss server can cause several security concerns.
How We Can Use .htaccess
The .htaccess is a file name and always presented as it is. This can be created in any of the text editor like textpad, ultraexit and similar.
Let us try to understand this by taking a simple example:
AuthName "Member's Area Name"
ErrorDocument 401 /error_pages/401.html
AddHandler server-parsed .html
In the above example, if a user fails to login correctly; it enables password protection to the directory which offers redirection to a custom error page. After creating the access file as shown above after this, you just have to upload this on web content site; this can be done simply by using file transfer protocol program. Along with .htaccess files one must have to upload the file in ASII mode. Probably your FTP software will default to 'BINARY' so look for a 'Transfer Mode' or 'Transfer Type' option in the menu. While uploading .htaccess file it may not appear in directory listings for files in the website which indicates your server or FTP software is hiding them. Setting incorrect file permissions on the .htaccess file can be a possible cause of error. This only takes place on certain servers, but you may like to alter the permissions on the file to 'executable' or '755'. With the help of your FTP software you can easily do this, look for a 'CHMOD' or 'File Permissions' option, and input '0755'. When you are having issues with your .htaccess file or it is not working, you should contact your web hosting company or system administrator and make sure that they have enabled .htaccess within your account. You have to take permission of web hosting companies prior to use in some cases. If errors persist, you should contact your system administrator for advice.
Some Implementations of .htaccess file
Customize Error Pages:
We can set or customize our own, particular error pages (for example internal server error or when a file is not found) instead of not having an error page or using those boring default error pages. By doing this we can make our website seem much more professional.
You can customize error pages for any error, if you know its number (for example 404 is for page not found). You can do this by adding the following to your .htaccess file:
ErrorDocument errornumber /file.html
A directory index can be stopped from being shown with help of .htaccess
Sometimes, for some reasons, you do not have index file in your directory. It means, if someone types the directory name into their browser, a complete list of all the files will be shown that are present in that directory. This can be a major security risk for website.
There are various useful functions of the .htaccess file and one of them is, you can redirect request to different files, either on an entirely different web site or on the same server.
This can be really useful in allowing the users to find the files (though you have changed the name of one of your files). Another use is redirection to a longer URL.
For example in your newsletters you can use a very short URL for your affiliate links. You can redirect a particular file by doing this:
Redirect /location/from/root/file.ext http://www.othersite.com/new/file/location.xyz
In this above example, a file in the root directory (oldfile.html) would be go in as:
and a file in the old subdirectory would be go in as:
With the help of .htaccess file, you can redirect whole directories of your site too. For example, a directory called olddirectory was present on your website and you had set up the same files on a new site at: http://www.new-site.com/newdirectory/
Then you don’t have to specify each file and you can redirect all the files present in that directory by doing this:
Redirect /olddirectory http://www.newsite.com/newdirectory
Then, any request to your site below /olddirectory will be redirected to the new site.
For example if someone type:
They would be redirected to:
You can deny/allow certain IP addresses with help of it
Sometimes, you may want to give permission to access your website to people with specific IP addresses only (for example, you can only allow people using a certain ISP to get into a particular directory) or you may want to prohibit certain IP addresses (for example, you can keep troublemaking members out of your message boards). But, this can only work if you know the IP addresses you want to ban. This is not permanently the best way to limit usage because most people on the internet have a dynamic IP address now.
Block an IP Address
For example you may want to ban an IP address 000.000.000.000;
you can block that IP address by using:
deny from 000.000.000.000
deny from 000.000.000.000 #specify a specific address
deny from 000.000.000.000/30 #specify a subnet range
deny from 000.000.* #specify an IP address wildcard
allow from all
Allow an IP address
You can also give permission to specific IP addresses to access your site. For example you may want to give permission or allow an IP address 000.000.000.000; you can allow that IP address by using:
allow from 000.000.000.000
allow from 000.000.000.000 #specify a specific address
allow from 000.000.000.000/30 #specify a subnet range
allow from 000.000.* #specify an IP address wildcard
deny from all
Alternative Index Files
We don’t always want to use index.html or index.htm as our index file for a directory, for example if we are using PHP files in our site, we may want to use index.php index file for a directory. We are not restricted to 'index' files though. Using .htaccess we can set abc.xyz to be our index file if we want it to be our index file!
There will be a list of alternate index files. The server checks to see if each file exists by working from left to right and it will display a directory listing if none of them exist (unless you have turned this off, of course).
Directory Index index.php index.html index.php3 messagebrd.pl index.htm
About The Author: Hi! I am Neelam Y. I am passionate about research and technology. Whether it is website designing/development, content writing or internet marketing; I have a solid track record of delivering utmost satisfaction to my clients. If you want me to provide you the finest of my services, you can contact me through the link http://neelam.i60.us/?i=1 or on upwork.com by following link: https://www.upwork.com/users/~01ea293264ce65c7ea