Skip to Content

How to redirect Apache root directory to virtualhost directory?

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.

1. Enable the rewrite and alias module in Ubuntu.

root@ubuntu ~]# a2enmod rewrite alias

root@ubuntu ~]# systemctl restart apache2

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’.

@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>

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

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

                RewriteEngine On
                RewriteRule ^$ /ezlogin/


4. Restart Apache webservice

@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.