Introduction to the .htaccess file

ยท

3 min read

The HT in the acronym stands for "HYPERTEXT" in case you are not aware. The .htaccess file is a web server config file that mostly runs on an Apache web server, it is used for writing configuration rules that instructs the web server how to handle a website. Using this file is not necessary though if you have access to directly edit your server configurations because it can hurt the performance of your site.

Let me give 2 analogies of how this file works

  • Take Wordpress for instance, if you are familiar with the platform, one of its best practices is to create a child theme from a parent theme so that any change you want to make would only affect the child theme and not the parent theme. In the case of the .htaccess file, you can say it is the child theme and the parent theme is the configuration settings of your server.

  • Another example would be a television and a remote. you can change the channel, increase the volume or even change the settings of the TV from the remote control without having to directly do it from the buttons on the television. This is the exact same idea of how the .htaccess file operates, you are changing the configuration settings on the server (remote) without having to directly do it from the settings of the server (TV).

I hope this analogies have helped you understand why you need to use this file on your server, instead of doing it the traditional way.

I spoke about the .htaccess file being able to change/modify settings on your server for you, let's take a look at some of the functions this file helps you change

  • URL Redirects - This is a feature that can be implemented from the settings of your web server but as earlier mentioned, if you don't have access to it, you can use the .htaccess file to still do it. To do this, you specify what are called RULES. For example if you have a url on your e-commerce web app that points to a page for storing products, you can specify a rule in your .htaccess file that instructs the web server to redirect any request made to this url example.com/store.php to example.com/store. The URL is still the same but the only difference is we removed the .php from it. This type of technique helps to improve your SEO and makes your website easier for web crawlers to index.

  • Loading custom pages - You can use this file to load up custom pages like 404 pages in situations where the user where to enter an invalid URL not recognized by the website. You can instruct the web server what page to load when a certain situation occurs on your website.

  • Force enable HTTPS - It is possible to use this file to make sure your website can only load from a secure protocol (HTTPS). You could argue that this is also possible by using an HTML meta tag, but doing it from the server is a better way to ensure that your website always loads securely because afterall, most times, your website is being loaded from a server so why not have it enforced from there?.

  • Blocking of IP Addresses - You can even specify certain ip addresses you want to block from having access your website, this is quite cool and very good for security. In a case where you detect any fradulent or suspicious act from a particular users location, you can block their ip address from being accessible to your website.

Hope you enjoyed reading this, i would release the second part of this post explaning certain commands of this file. You can find more about me on Twitter and Linkedin.