Free localhost virtual domain on Windows OS

To use a virtual domain like www.somedomain.com on a Windows operating system and the application XAMPP or other website server for Windows operating system do the following:

In the path of your Windows operating system:  C:\Windows\System32\drivers\etc   there you can find the file called: host
Add the following to that file host:

# localhost XAMPP virtual domain:
    127.0.0.1    domain.com
    127.0.0.1    www.domain.com

# localhost XAMPP virtual domain:
    127.0.0.1    mydomain.com
    127.0.0.1    www.mydomain.com

In the root path of your XAMPP website  C:\xampp\htdocs  add the following script inside an new index.php file:

<?php
        //    Local host virtual domain script
        if (!empty($_SERVER['HTTPS']) && ('on' == $_SERVER['HTTPS'])) {
            $uri = 'https://';
        } else {
            $uri = 'http://';
        }
        $uri .= $_SERVER['HTTP_HOST'];
        //    header('Location: '.$uri.'/dashboard/');
        ### See "C:\Windows\System32\drivers\etc\hosts" file for the virtual domains like below.
        #    In host file by example:
        #
        # localhost XAMPP Virtual Domain
        #    127.0.0.1    domain.com
        #    127.0.0.1    www.domain.com
        #
        if(isset($_SERVER['HTTP_HOST']) and $_SERVER['HTTP_HOST'] == "domain.com" or $_SERVER['HTTP_HOST'] == "www.domain.com"){
            header('Location: '.$uri.'/website1/');    //    C:\xampp\htdocs\website1
        }elseif(isset($_SERVER['HTTP_HOST']) and $_SERVER['HTTP_HOST'] == "mydomain.com" or $_SERVER['HTTP_HOST'] == "www.mydomain.com")
            header('Location: '.$uri.'/website2/');    //    C:\xampp\htdocs\website2
        }else{
            header('Location: '.$uri.'/home/');
        }
        exit;
?>
Something is wrong with the XAMPP installation :-(

Assume /website1/ and /website2/ in the example index.php file above is the directory of your websites.
You can use the localhost virtual domain even when you are disconnected from the Internet.

Remember the virtual domain may not exists on the Internet when you are connected to the Internet, if it exists on the Internet and want to use your virtual domain, disconnect the Internet connection and load your virtual domain like a normal registered domain on the Internet in your website browser. 

Enjoy!




. . .