How To Create .htaccess file For Your Website

.htaccess‘ is the filename in full; it is not a file extension. It means Hypertext Access. The .htaccess file is the configuration file used by web servers to control the behavior and performance of a site. It gives instructions to the server and provides a way to make configuration changes on a per-directory basis especially in situations where you don’t have access to the main configuration files of the webserver e.g. apache.

The common uses of the .htaccess file include

Setting another file as your index file, e.g. instead of using index.htm or index.php you can use an alternate file like home.php

Creating Custom Error Pages, for example, to have a personalized error page that shows up when a link is broken or file is not found. A file 404.htm is created and placed in the root directory of the website. You can use the 404.htm file as the custom error page for a 404 error by using the command:

ErrorDocument 404 /404.htm

We can also customize pages for other HTTP errors like 500, 400 and 403

Stopping a Directory Index From Being Shown because it’s usually a security risk when it is possible for someone to type the folder name into their browser and see a full listing of all the files in that directory.

Allowing you to create Redirects from a specific page to a new page you can even create redirects to a new website entirely or redirect from one folder to another. We mean, 301 permanent redirects.

It is also possible to Password Protect certain directories/folders on a website using the .htaccess .

Creating a .htaccess File

You can create a .htaccess file on your local computer with any good Text editor such as Notepad. You do this by simply opening Notepad, then save the file as .htaccess.

Edit and add Commands

Your .htaccess file could serve numerous purposes, it could contain a block of commands or it may simply contain one line of command depending on how many Web Server functionalities and features you are using. Before entering commands into your new file, make sure that Word Wrap is turned off, this is important because most .htaccess commands need to be placed on one line only. I would not enlist all possible htaccess commands here, if you need a specific feature you can Google the command.

Upload

After you have created your file and put in the necessary the htaccess commands, you will need to use an FTP client to upload the htaccess file to the server. Additionally, the file must be uploaded as ASCII mode, not BINARY

Note: After uploading the file it may be hidden and you will not be able to see it in the cPanel if show hidden files is not enabled.

The placement of the .htaccess file is important because a .htaccess file will affect everything in the directory it is placed in and all resulting sub-directories. In my opinion its best you add your ‘.htaccess’ file to the ‘web site root’ if your configuration affects the whole website.

Additionally, htaccess needs to be writable by the webserver so you may want to change the permissions on the. htaccess file to ‘644’ or you could also use a .htaccess Generator if you need help creating your htaccess file.

This was just a brief overview. If you need any further enlightenment I would suggest SitePoint

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.