What is .htaccess and why is it so important
Even though .htaccess is just a file, it has a huge importance for your website and its contents. It could change the settings of your web server and you could do a lot more with it.
Most popular uses of .htaccess files are:
- Completely customized 404 Error pages
- 302 redirects
- Allow or deny settings to user groups
- Forbidden setups
- Password protection of folders
- Change file extensions
- Banning users with certain IP address
- Using a different index file
- Hide folders and files inside it on your web server.
First find out if htaccess is allowed by your hosting provider
Usually most of the hosts allow htaccess and if it’s a Linux server with apache as the web server ideally it should allow the usage of the file, further it depends on the permissions that your host has provided on your web hosting to let you or not let you use it.
How to create an .htaccess file
Simply open a note pad, paste the contents of your intended .htaccess file and save as “all file types” and name file with extension .htaccess and upload it to your ftp in root of your website.
Usage 1: Create Custom error pages
This is one of the main and simple usage of .htaccess files – you can create completely customized and more professional error pages and use them instead of the default one provided by your web host. I bet you would have wondered if I could change the crying alligator error message and have my own. Well find how to do it below.
As long as you know the error number you could use it for any error page.
The syntax for usage is as below:
ErrorDocument errornumber /file.html
For example: ErrorDocument 404 /customerror.html (for a 404 error)
Some of the commonly found errors are:
These are some of the most common errors:
401 – Authorization Required
400 – Bad request
403 – Forbidden
500 – Internal Server Error
404 – Wrong page
Simply add the code to your website .htaccess and upload the customerror.html page you want to display in each case
Usage 2: Hide listing of directory index
In some case when some user accidently types in a sub directory name in the browser of your web server, it lists the folders and files under it and this is serious security vulnerability. This vulnerability can be closed with the help of a simple .htaccess file
Just add a new line to your htaccess file
Options –Indexes
Usage 3: Banning users or spammers from specific IP address
In some cases you would want only specific IP address to use or not use your directory or website, in such a case add the below line to block an IP address.
Deny from xxx.xxx.xxx.xxx
Example:
Deny from 169.189.3.2 – This will block the specific IP address
Deny from 169.000.000.000 – This will block the specific IP range post 169
Usage 4: Usage of alternate index files
Usually in a root web directory or any web directory the index file would be either index.html or index.htm or index.php or default.htm
What if you wanted to change this to another one say whiskey.beer?? Yes htaccess has the answer! It could even change the default file name as well as the default extension.
Alternate index files can be used with the below line added and the server would read from left to right and skip if file not present and use the next file as default. Below is the syntax
DirectoryIndex index.htm whiskey.beer index.html
Usage 5: Simple Redirections
This is one of the most useful functions of the .htaccess file you could easily set redirections from one file to another, one directory to another or even one website to another.
In case you want to redirect the root index.html file to another folder or
Redirect 301 index.htm http://www.samewebsite.com/newfolder
Or Redirect 301 index.htm http://www.anotherwebsite.com/newfolder
Or Redirect 301 index.htm http://www.samewebsite.com/anotherfolder/anotherfile.htm
OR Redirect /olddirectory http://www.anotherwebsite.com/newdirectory
Redirects note: File name is simply used at the first half of the command for redirects when the .htaccess is in the same folder as of the first file.
Note: These syntaxes could be simply added to your .htaccess files as a simple line and then uploaded to find the effect.