.htaccess is a configuration file used on web servers running the Apache Web Server software. The server looks for specifically named files as the first page of your website, also known as the index page. For example, if you were to ask for www.example.com/about/, a typical web server will look for a file called
index.html in the
about folder of your website. If it exists, the server will deliver that page’s content to the browser. You can change the name of the index file your account looks by using
directoryindex rule in your
What you’ll need
Before you begin this guide you’ll need the following:
- Access to Hosting control panel or FTP
Step 1 — Locating/creating and accessing your .htaccess file
First thing you will need to do is locate or create the .htaccess file. This can be easily achieved using the File Managers located in your hosting control panel or by using a FTP client such as FileZilla. In this tutorial, we will use Hostinger’s File Manager.
You will be required to edit your existing .htaccess file or create a new one if it does not exist. An existing .htaccess file can be easily changed by clicking the right mouse button and selecting the Edit function from the dropdown menu as shown below:
If .htaccess does not exist, you can quickly create it by right-clicking on an empty area inside File Manager and selecting the New File function.
After that, you will be prompted with a file creation box, where you will need to enter the new file’s name. In this case, you will need to type
.htaccess and press Create to save the file. Once that is done, you will be able to edit the newly file and add your code.
In case your hosting platform uses cPanel, you can reach the same goal by following this tutorial:
Step 2 — Editing .htaccess file
Once you have opened your .htaccess file using Edit function, paste the following code at the top of the page to configure your desired index page:
In our example below, we decided to make the index page of our folders named
first.html, which means that the code in .htaccess should look like this:
You can also list more than one file in the configuration. Your specified index files will be read and checked from left to right. In this example, we add
index.php to the list. First, the server will check for
first.html, if it does not find a file with that name, it continues to
index.htm and so on.
DirectoryIndex first.html index.htm index.html index.php
This can also have some nice practical uses. For instance, you may set up a temporary page where your visitors will be directed to in case your website unexpectedly crashes. Here is an example code:
DirectoryIndex index.php page-down.php
When using this configuration, the browser will first look for
index.php. In case it may not be accessed, the next file to load would be
page-down.php. Once the desired code is added, don’t forget to Save the changes and the configuration will start taking effect.
Congratulations! You have successfully changed the default index page of your website by manipulating the .htaccess file.
In this tutorial, we have learned how to effectively change the first page on your website using .htaccess. We’ve also learned a few practical tricks that can benefit every developer at some point in time. In case you have any cool tips that you want to share with us, you are welcome to do so in the comments.