htaccess file , how it is used

Published on 25 Apr 2021

Best & Reliable Website developers in Nairobi

Introduction

As a website developer or a user who visits web pages commonly known as websites, you might have sometimes observed a common behavior in the link at the address bar. What do I mean? In all the websites domain you visit, for example, www.google.com, the address bar adds a pre-fix HTTP:// and in others HTTPS://. My focus today will be on those prefixes, why they are added and why they differ between one webpage and the other.

What is an htaccess file?

Htaccess a short form for ‘hypertext access’ is a default name of the file found in web servers that are used to set server configurations for specific files in the directory the file is residing. Just like how the ‘index’ file is the default file that the server fetches when it loads, the same case happens when the web server wants to check whether there are special configurations that the files should obey before they load in the browser.

The htaccess usually starts with a dot (.) to mean that it is a hidden file and it signals the apache that it is not a normal file for a website.

Where is the .htaccess file found?

In web servers, for those who use CPanel (a web hosting control panel software developed by cPanel, LLC) it is found in the public_html directory though by default it is always hidden.

To view it

  • On login into the Cpanel, navigate to the files section
  • Click on the File Manager
  • Click on the public_html folder
  • Inside the public_html folder, on the top right there is a settings section
  • Click on the settings and a popup modal will appear
  • Check on the Show Hidden Files and click the save button
  • The popup modal will disappear and the public_html directory refreshes
  • Scroll down and the .htaccess file will be visible
  • Right-click on it and click edit

 

What does htaccess do?

From our definition of htaccess, we highlighted that it is a configuration file that sets controls of files located in the directory where the file is found. So, the primary function of the htaccess file is to define what files can do and what they cannot.

For example, the htaccess has the power to force a page to load HTTPS other than HTTP in a way that forces the files to add the enhanced security feature in the hypertext transfer protocol.

 

Is it necessary in web development?

In the web development cycle, the htaccess file is necessary as it is to have an index file. the htaccess file allows one to set not only one configuration or instruction but many which will guide the files.

Some functions include;

  • Forcing links to redirect from http to https even if the SSL is not valid
  • Removing the file extension example, .php, .html. .py
  • When a page is not found, it redirects to 404 custom page
  • Blocking access to some resources in the directory
  • Denying access depending on the roles of a user

How many htaccess files should I have?

The number of htaccess files is not defined and cannot be limited to a specific number. What is necessary is for the web developer to note and track the level and function of each htaccess file. One should remember, the htaccess file in the public_html directory affects the entire web directory and should therefore be treated as the main file, others can be part of the subdomains and others in subfolders of the main directory. Each htaccess affects the files inside the directory where the file is residing. My recommendation, for a website that is not having sub domains and the one that is designed for data display, one htaccess file is enough.

Conclusion

The discussion above has highlighted what is an htaccess file and what it can do. Therefore it is necessary to note, if you see a website not behaving as you expect, please always be sure to check on the .htaccess file and see whether there are some configurations done that either need to be changed or removed.

To ensure that the changes made in the file are working, always force refresh the browser to remove the cache, in Microsoft windows use CTRL+SHIFT+R.