How To Change Default Index Page in Apache

Last updated on July 8th, 2024 at 08:36 am

When we install Apache server, it contains a default index page that is loaded when we open web browser and visit our IP or domain. It just says ‘It Works’ and nothing else. But for every website, you will eventually need to change the default index page to the home page of your website/blog. In this article, we will look at how to change default index page in Apache Web Server.

How To Change Default Index Page in Apache Web Server

Here are the steps to change default index page in Apache web server. You can change default index page via Apache Server configuration file, or using .htaccess file. We will look at both approaches below.

Using Apache Configuration File

1. Backup Apache Configuration

Apache configuration file is present at one of the following locations depending on your installation:

  • /etc/apache2/httpd.conf
  • /etc/apache2/apache2.conf
  • /etc/httpd/httpd.conf
  • /etc/httpd/conf/httpd.conf

Run the following command to take its backup. Update file path to Apache configuration file.

$ sudo cp /etc/apache2/httpd.conf /etc/apache2/httpd-backup.conf

2. Open Apache Configuration File

Open terminal and run the following command to open Apache configuration file

$ sudo vi /etc/apache2/httpd.conf

3. Change DirectoryIndex Directive

You will see the following lines of code. Look for DirectoryIndex directive.

#
# DirectoryIndex: sets the file that Apache will serve if a directory
# is requested.
#
<IfModule dir_module>
    DirectoryIndex index.html index.php
</IfModule>

Change index.html index.php to your choice of web page (e.g home.html). It is important to remove index.html index.php unless your home page has same file name.

<IfModule dir_module>
    DirectoryIndex home.html
</IfModule>

Make sure you have placed this file home.html at /var/www/html/. This is the directory where Apache will look for index page, when users request your website’s domain. If you have placed it in a different folder (e.g /var/www/html/product/) then modify the path above accordingly (e.g /product/home.html).

<IfModule dir_module>
    DirectoryIndex /product/home.html
</IfModule>

You can also add multiple index pages to Apache as shown below. In this case, Apache will try loading the pages one by one, from left to right. In case any page is not available, the page mentioned after it will be loaded.

<IfModule dir_module>
    DirectoryIndex home.html welcome.html
</IfModule>

4. Restart Apache Server

Restart Apache web server to apply changes.

$ sudo service apache2 restart

Using .htaccess

Often site administrators do not have access to Apache configuration file, or they do not want to change it. In such cases, you can use .htaccess file available after enabling mod_rewrite module in your Apache server. You can also change default index page for Apache using .htaccess. It is commonly used to redirect one page to another in Apache server. Before proceeding, please enable mod_rewrite (.htaccess) in your Apache web server.

1. Open .htaccess File

Open .htaccess file, typically located at /var/www/html/.htaccess

$ sudo vi /var/www/html/.htaccess

2. Add DirectoryIndex

Add the following line to .htaccess file to set index page to home.html.

DirectoryIndex home.html

You can also mention multiple pages, in case you want to load another page, if one is not available. In this example, home.html will be loaded first. If it is not available, index.html will be loaded.

DirectoryIndex home.html index.html

3. Restart Apache Server

Restart Apache web server to apply changes.

$ sudo service apache2 restart

4. Verify Changes

That’s it. Open browser and visit http://your_server_or_ip and you will see the new page. Replace your_server_or_ip with your domain name, or server IP address.

Conclusion

In this article, we have learnt how to change default index page of Apache server. It is essential to point it to your website or blog’s home page. It is best to enable mod_rewrite on your Apache server and make changes in it, instead of modifying Apache server config file. Otherwise, your users will continue to see the default Apache page.

Ubiq makes it easy to visualize data in minutes, and monitor in real-time dashboards. Try it Today!

Also read :
How to Install mod_security on CentOS 7
How to Set Up Virtual Hosts in Apache
How to Change Timezone in Apache/PHP