How To Use a .htaccess File

What is the .htaccess file?

The .htaccess file is a configuration file for web hosting account settings. It tells the web server how the account or website is configured. What web dependanices to use and what not to use, it is used to override the default web server configuration. It’s a very powerful tool. Many content management systems also use the .htaccess file to configure the website. We can create a .htaccess file and configure or modify it manually.

Why do you Need a .htaccess file?

A .htaccess file can assist in changing the way default setting of a web server and its behave. You can create customized directives and place them in this file in order to alter the basic web server behavior.

The .htaccess file is commonly used to:

  1. leverage browser caching for a website’s assets like: image files, CSS files and JavaScript files.
  2. enable or disable Gzip compression on the web server.
  3. to do permaneent htaccess redirects (301) and much more…

What do you need to use the .htaccess file?

The .htaccess file is usually located in the root folder of your web hosting account. You can access or create the file using either your FTP login credentials or through the file manager of your web hosting account. You will need to use a plain text editor like notepad or notepad++ to create or edit the file.

Creating a .htaccess file for your website

The .htaccess file is a plain text file. This means that you can create it using any text editor like TextEdit on Mac, and notepad or notepad++ on Windows. You can also directly create the .htaccess file using your web hosting panel or even using a ssh client through shell access. While it is quite simple doing so using the web hosting panel or via ssh, when you are creating a .htaccess file in Windows, you need to take care and save it as htaccess.txt first and then upload and rename it to .htaccess. This is due to the fact that Windows treats all file names starting with a dot as system files. Also, by default, Windows keeps the system files hidden from users. Thus, they are not visible to users.

Creating and Uploading .htaccess file in Windows

The .htaccess file can be created as a normal text file. You can use your default text editor to create one. With Windows, the two most common text editors available are notepad and notepad++. Use any one or any other text editor of your choice. A special format and style is used to write the content of a .htaccess file. The format is what the web server understands and needs. I will be covering what format you can use and what functions of Apache and Nginx you can modify using the .htaccess file in a future article.

For now, all you need to understand that within Windows, you will first save the htaccess file in a text format and name it htaccess.txt and once you upload the file to your web hosting server you should rename it to .htaccess.

How to Upload the .htaccess file to your Web Server

You can use FTP / SFTP to upload the .htaccess file to your web hosting server. At this point it is important to upload the file to the folder who’s Apache / Nginx settings you want to be modified. If you upload the file to the root directory, it will impact all files and folders on your hosting account. For the purpose of this tutorial, we will want to upload the .htaccess file to the main directory of the webiste so that it affects all sections of the website. An example location of your web hosting’s root directory in cPanel is: /home/your-hosting-ac-name/public_html/ for other web hosting panels it may vary. The important thing to note is that most of the web hosting panels provide a /public_html/ or a /hhtpdocs/ folder and you will want to upload the file there.

A .htaccess file has the capability to override almost all the default web server’s settings including PHP settings. Thus a .htaccess file comes handy to modify. For example; set the PHP version (if there are more than one version of PHP installed on your web hosting server). It also helps you to change most of the PHP values also. In order to change PHP values, you can preface them using the php_value variable.

I will be covering all the different setting changes that you can do with a .htaccess file in my future articles.

What are other things you can control with the .htaccess file

Reporting errors

Apache and Nginx have the capabilities to report errors on the front end, however by default these settings are always set to hide. By placing a simple line of code inside the .htaccess file, you can specify to the web hosting server that you want the web server to report the errors on the front end. This function helps in debugging both http and PHP errors. For an example, the line “php_flag display_errors on” tells Apache to show all php errors in the front end.

Changing the default directory index

Sometimes you may want to use a custom directory or file as your default index. in that case, you can specify the same to the web server by placing the directive DirectoryIndex inside the .htaccess file.

Further Reading and Documentation

Please refer to or for more details and documentation on what aspects of the web-server you can manage with the .htaccess file.



Please enter your comment!
Please enter your name here