Oct 09, 2019
How to Change the Default Index Page Using directoryindex and .htaccess
In this article, we will show you how to change and modify the first page of your website using the directoryindex command in the .htaccess file. Let’s check it out!
How Does it Work?
When you open a website on any browser, a typical web server will look for a file called index.html or index.php in its public_html files to load and deliver that file’s content to your browser.
directoryindex is a command that lets you modify the index files of your website easily through .htaccess file. It’s used to specify which files should be loaded first by the page when your website is accessed.
You can also set up a second, third, and other files to be loaded by the servers next in case there’s any error causing the designated first page to load poorly.
Locating and Accessing the .htaccess File
The first thing you need to do is locate or create a new .htaccess file in the WordPress root directory or in our case, public_html. Here’s how:
- Login to Hostinger’s hPanel and navigate to the File Manager located under the Files area.
- You can also configure an FTP client such as FileZilla to access the directory from a local computer.
- Once you’re in public_html directory, scroll down and locate the .htaccess file. If it doesn’t exist, you can quickly create one by going to the upper part of the page and selecting the New File function.
- Once you’re prompted with a file creation box, enter .htaccess, then press Create to save the file.
It’s worth mentioning that locating the .htaccess file on cPanel follows a similar process.
Editing .htaccess with the directoryindex Command
To modify the first page loaded by your website using the directoryindex command, follow these steps:
- Head to .htaccess and right-click to Edit.
- Paste the following code at the top of the file to configure your desired index page:
Remember to replace example.html with the page you want to use.
You can also list more than one file in the configuration. In this example, we add first.html, index.html, and index.php to the list.
Your specified index files will be read and checked from left to right. First, the server will look for first.html, and if it doesn’t find a file with that name, it continues to index.html and so on.
You can also use this command to set up a temporary error page where your visitors will be directed to in case your page unexpectedly crashes. Here’s an example:
DirectoryIndex index.php page-down.php
Once the desired code is added, don’t forget to save the changes, and the configuration will take 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 directoryindex along with the .htaccess file. In case you have any other cool tips that you want to share with us, you are welcome to do so in the comments!