Skip to Content

How to redirect Apache root directory to virtualhost directory?

Redirecting Apache to a VirtualHost Using .htaccess


Overview: This article explains how to redirect Apache's default document root to a virtual host directory using .htaccess on Ubuntu 18, 20, and 22 by enabling necessary modules, modifying Allowoverride, and restarting Apache.


The objective of the article is to redirect from default document root (/var/www/html/) to another virtualhost directory using .htaccess in Ubuntu 18, Ubuntu 20, Ubuntu 22.

Step 1: Enable the rewrite and alias module in Ubuntu.

root@ubuntu ~]# a2enmod rewrite alias

root@ubuntu ~]# systemctl restart apache2

Step 2: Adjust the Apache configuration files to allow the use of .htaccess files. By default, the ’AllowOverride’ directive is set to ’None’ for the /var/www/ directory in Apache configuration files. You need to change it to ’All’.

root@ubuntu ~]#sudo nano /etc/apache2/apache2.conf

Inside the file, locate the <Directory /var/www/> section.

<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>

Step 3: Create a new .htaccess file inside /var/www/html directory if it doesn’t exist and add the following parameters.

root@ubuntu ~]#cd /var/www/html/
root@ubuntu ~]#sudo vi .htaccess

                RewriteEngine On
                RewriteRule ^$ /ezlogin/

Step 4: Restart Apache webservice

root@ubuntu ~]#sudo service apache2 restart

Before .htaccess redirection, Apache default page is displayed as shown below.

After implementing .htaccess redirection, IP is redirected to virtualhost where Ezeelogin is installed.


Related Articles:

Disable apache default page and replace with an error page.

Redirect IP to Domain Name in Linux

List Apache Modules in Linux