Hello everyone!

We have been investing plenty of personal time and energy for many years to share our knowledge with you all. However, we now need your help to keep this blog running. All you have to do is just click one of the adverts on the site, otherwise it will sadly be taken down due to hosting etc. costs. Thank you.

Post below shows us how to create a user friendly URL as http://test.com or http://www.test.com for our applications rather than seeing http://localhost/folder/another-folder/test in browser. Assuming that our web document folder is var/www/html on Ubuntu.

Current status

Currently we have "index.php" file in project folder "test".

inanzzz@inanzzz:~$ ls -l /var/www/html/test
total 4
-rw-rw-r-- 1 inanzzz inanzzz 21 Mar 28 08:22 index.php

Update hosts file

Add line below to "hosts" file, save and exit.

inanzzz@inanzzz:~$ sudo nano /etc/hosts
[sudo] password for inanzzz: localhost test.com www.test.com

Create a config file

We're going to replicate existing "000-default.conf" but will update its content later on.

inanzzz@inanzzz:~$ ls -l /etc/apache2/sites-available
total 12
-rw-r--r-- 1 root root 1332 Jan 7 2014 000-default.conf
-rw-r--r-- 1 root root 6437 Jan 7 2014 default-ssl.conf
inanzzz@inanzzz:~$ sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/test.conf
inanzzz@inanzzz:~$ ls -l /etc/apache2/sites-available
total 16
-rw-r--r-- 1 root root 1332 Jan 7 2014 000-default.conf
-rw-r--r-- 1 root root 6437 Jan 7 2014 default-ssl.conf
-rw-r--r-- 1 root root 1332 Mar 28 08:34 test.conf

Update config file

Update "test.conf" so that its content reads like below.

inanzzz@inanzzz:~$ sudo nano /etc/apache2/sites-available/test.conf

<VirtualHost *:80>
ServerAdmin your@email.com
ServerName test.com
ServerAlias www.test.com

DocumentRoot /var/www/html/local/test

<Directory />
Options FollowSymLinks
AllowOverride None

<Directory /var/www/html/local/test>
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order allow,deny
Allow from all

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

Enable the site/config

Command below adds symbolic link to "/etc/apache2/sites-enabled/" as "test.conf". To revert this process, use a2dissite in command.

inanzzz@inanzzz:~$ sudo a2ensite test.conf
[sudo] password for inanzzz:
Enabling site test.
To activate the new configuration, you need to run:
service apache2 reload

inanzzz@inanzzz:~$ ls -l /etc/apache2/sites-enabled/
total 0
lrwxrwxrwx 1 root root 35 Jan 13 22:56 000-default.conf -> ../sites-available/000-default.conf
lrwxrwxrwx 1 root root 33 Mar 28 08:57 test.conf -> ../sites-available/test.conf

Restart Apache

To activate what we have done so far, Apache needs to be restarted.

inanzzz@inanzzz:~$ sudo service apache2 restart
* Restarting web server apache2 [ OK ]


If you call http://test.com or http://www.test.com, your application should be accessible now.