Create Local VirtualHost For Your Development Environment

If you are using XAMPP, you can access your local server by going to http://localhost. Now I would like to share how to create your own local domain and add it to the Apache virtual host, so you can have a local website for development purposes. For example, you can have www.domain.local. To create this you only need to edit your Apache vhost and your hosts file.

If you are using Windows, you can edit your hosts file at C:WindowsSystem32driversetchosts. You can edit this file using Notepad with administrator rights (Run as Administrator). In this file you can add your local domain. See the example below:

127.0.0.1 www.ivankristianto.local
127.0.0.1 ivankristianto.local

After you create and save the file, you need to flush your DNS. You can use the following command from your command prompt:

ipconfig /flushdns

To test if your setting is working you can ping your new local domain — it should reply from IP 127.0.0.1.

After you edit your hosts file, you need to create your virtual host in Apache. If you are using XAMPP for Windows, it is usually at [your xampp folder]/apache/conf/extra/httpd-vhosts.conf.

Please add this template and change it as necessary to match your configuration:

NameVirtualHost *:80

<VirtualHost *:80>
    DocumentRoot "/path/to/htdocs/ivankristianto"
    ServerName www.ivankristianto.local
    <Directory "/path/to/htdocs/ivankristianto">
        Options Indexes FollowSymLinks MultiViews
        AllowOverride all
        Require all granted
    </Directory>
</VirtualHost>

Please check the path carefully and make sure it matches your application folder path. If you do it right, you can restart Apache, open your browser to www.ivankristianto.local, and it will show your website. With this technique you can do development more easily.