change default home page in .htaccess

How to Change Default Page in .htaccess

By default, Apache looks for index.html / index.htm / index.php file to load your website’s home page. In most websites, you will eventually need to change your default page. In this article, we will look at how to change default page in .htaccess for Apache web server.


How to Change Default Page in .htaccess

Here are the steps to change default page in .htaccess.


1. Enable mod_rewrite

If you have enabled mod_rewrite on your Apache web server, you can skip this step.

Ubuntu/Debian

Open terminal and run the following command

$ sudo a2enmod rewrite

Redhat/CentOS/Fedora

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

$ sudo vi /etc/httpd/conf/httpd.conf

Look for the line

#LoadModule rewrite_module modules/mod_rewrite.so

Uncomment it by removing # at its beginning.

LoadModule rewrite_module modules/mod_rewrite.so

If you don’t find the above line just add it to Apache configuration file.

Also look for <Directory> tag and change AllowOverride None to AllowOverride All

<Directory /var/www/html>
. . .
 # 
 # AllowOverride controls what directives may be placed in .htaccess files.
 # It can be "All", "None", or any combination of the keywords:
 # Options FileInfo AuthConfig Limit
 #
 AllowOverride All
. . .
</Directory>

Also read : .htaccess Make URL Case Insensitive


2. Create .htaccess file

If you have already enabled mod_rewrite before you will find a .htaccess file at your website’s root location /var/www/html. Else you can create it afresh.

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

Also read : .htaccess Make Subfolder Root


3. Update default page for your website

Let us say you want to set /home.html as your new web page and it is located at /var/www/html/home.html. In such case, add the following line to your .htaccess file.

DirectoryIndex home.html

Please note, if your page is located in a subfolder (e.g. /data/home.html) then you need to mention it in the DirectoryIndex ( e.g. DirectoryIndex /data/home.html)

Also read : How to Make Directory Inaccessible in Apache


4. Restart Apache Server

Restart Apache server to apply changes.

$ sudo service apache2 restart

That’s it. Open web browser and visit your domain (e.g http://www.mydomain.com) and you will see your new home page’s content on it.

In this article, we have looked at how to change default page for your website using .htaccess.

Also read : How to Remove Index.php from URL using .htaccess


Leave a Reply

Your email address will not be published. Required fields are marked *