Articles Comments

Complete, Concrete, Concise » Web Tools » How to Change ‘localhost’ to a Domain Name

How to Change ‘localhost’ to a Domain Name

This tutorial is for XAMPP 1.7.3 running under Windows XP with Service Pack 3. It may be similar for other version combinations, but no guarantee is made.

Assumes you have installed XAMPP. Instructions can be found here.

UPDATE 17-March-2012:If you are running Windows 7 (and possibly Vista) the default security settings prevent you from running applications with administrative privileges. This means that you won’t be able to save changes made to the hosts file.

For instructions on opening the hosts file for reading and writing please see this article.

Installing XAMPP allows you to develop, test, and play around with web server code on your local machine. You can configure and test out your website locally, instead of live somewhere on the web.

It does have the drawback that to access your website you have to type localhost/my-web-site, where my-web-site is actually the directory you installed your website in.

You can setup the Apache Server in XAMPP to serve up web pages as though they were actually located on www.my-web-site.com instead of localhost.

This is a two step process: first, you have to redirect the web site name to your computer; next, you get Apache to redirect the web site address to your installed directory.

Redirecting the Web Site to Your Computer

When you type a web site name into the URL field (say, www.google.com), your browser sends the name to a Domain Name Server that looks up the text name (www.google.com, for example) and returns the Internet Protocol (IP) address for it (74.125.39.104 is returned for www.google.com). Your browser then connects to IP address 74.125.39.104.

Before your browser queries the Domain Name Server, it first checks a file called hosts on your computer. If the requested web site name is found, it uses the IP address found in the file.

1) Locate your hosts file. In Windows XP, this file is located in C:\WINDOWS\system32\drivers\etc:

NOTE: Different versions of Windows may store the file in a different directory.

2) Open the file with a plain text editor (like Notepad++). Using a word processor, like Word, can mess up the file when you save it.

It is very likely that the file is empty, except for the default comment and examples (the section in the green box):

3) Add lines containing the domain name(s) and IP address to redirect to. See the section in the red box in the image above.

  • Each line must begin with the address 127.0.0.1 (this is the localhost address)

  • After the IP address, type the name of the domain you wish to redirect to this address. You may only enter 1 name per line.

  • Addresses like my-site.com and www.my-site.com are different and each need a separate line.

Your address does need to be a “valid” top level domain (i.e. .com, .net, .ca, .pl, etc). It just needs to be composed of letters (a-z), numbers (0-9), hyphens (- but not as the start or end of a name) and periods (.). In the examples above, I used .15, .16, and .17 as the top level domain (which are not valid top level domains).

You can redirect existing domains to your machine. If you entered the line: 127.0.0.1 google.com, then every time you typed google.com into your URL field, your browser would redirect the request to your machine instead of going to Google’s web site. NOTE: this redirection only happens on your computer – everyone else in the world will still go to Google’s home page.

You can also redirect (on your computer only) one website to another. For example, suppose you are boycotting Google, so you decide to redirect google.com to bing.com instead. You would do that by entering Bing’s IP address in your host file followed by the web site you want to resolve to that IP address: 195.57.152.178 google.com

4) Save the changes.

5) Restart your browser (if it was open when you made the change to your hosts file).

Getting Apache to Serve Your Web Site

These instructions are for XAMPP 1.7.3. Other versions or XAMPP, or different Apache Server installations may have the files in different locations, or may even have the files named differently.

6) Locate the httpd-vhosts.conf file. For most XAMPP 1.7.3 installs this will be C:\xampp\apache\conf\extra (in my case, I installed XAMPP on my D-drive, so the folder is D:\xampp\apache\conf\extra):

7) Open the file using a plain text editor (like Notepad++). Using a Word processor, like Word, can mess up the file when you save:

The file will contain some documentation and sample virtual hosts.

8) Add, at the bottom of the file, new virtual hosts for each domain you added in step 3) above.

Each host will have the following form:

<VirtualHost *:80>
    ServerName www.joomla.16
    ServerAlias joomla.16
    DocumentRoot d:/xampp/htdocs/joomla16
</VirtualHost>

ServerName: following this label, add the name of the domain you added in step 3)

ServerAlias: following this label, you may add as many alternate domains (separated by spaces) as you like that are supposed to resolve to this virtual host. Remember that www.my-domain.com is different from my-domain.com, but, typically, you would want them to resolve to the same server.

DocumentRoot: following this label, add the path where your website is located. You can use an absolute path (as in the example above), or a relative path (but relative paths can be trickier to set up, so I prefer to use an absolute path).

9) Save the file.

10) Restart the Apache Server to have the change take effect. Do this by clicking on the Stop button on the XAMPP control panel and then clicking on the Start button. (You could also just reboot the computer, but that is overkill).

You should now be able to type the domain name into your URL field and go directly to your locally hosted web site:

For more information on configuring virtual hosts, you can read the documentation at the Apache Foundation here.

There is a lot more you can do with virtual hosts (such as adding an IP address).

Budget web hosting providers use virtual hosts to host multiple websites on the same server (and same IP address).

Please consider sharing this article if you liked it or found it useful.

You may use one of the buttons below or share using your own favourite media.

Please do not republish this article on your website. You may publish a short excerpt and provide a link back to the original article.

Filed under: Web Tools · Tags: , , , , ,

23 Responses to "How to Change ‘localhost’ to a Domain Name"

  1. Bob Brown says:

    Thanks for this post! Useful.

    I have a question about the second half of this tutorial regarding the Virtual Hosts in Apache.

    It seems whether I set the Virtual Host or not it doesn’t make any difference.

    In my hosts file I put,

    127.0.0.1 dev.16

    In my browser I can access http://dev.16 with or without the virtual hosts setup in httpd-vhosts.conf

    Not sure exactly what that does? Can you explain?

    Thank you

    1. admin says:

      Thanks for your comment.

      I would guess that Joomla! or whatever site you are running on your local host is located in the root of the htdocs directory. In other words, if you typed in localhost (or 127.0.0.1) into your browser’s URL field, you would go to your website instead of the XAMPP local host homepage (assuming you are running XAMPP).

      The virtual hosts setup is needed to redirect the URL to the sub-directory hosting your site. For example, your site might be located in /htdocs/mysite/….

      Your web server (whether Apache or IIS or something else), serves up web pages from a specified directory. For XAMPP, this directory is xampp/htdocs, for AMPPS, the directory is /ampps/www, for IIS it is usually c:\www. Those are the directories the server looks in when you enter localhost (or 127.0.0.1) into your browser’s URL field.

      Often times, in that directory is some sort of management console for your server / mysql / php / perl installation. Web sites are usually installed into subdirectories, this is why you need to type localhost/mysite instead of just localhost.

      I hope that helps clarify things.

      1. kishore says:

        Hi,

        Thanks for your valuable document. you explained that the change can done in one pc only. but i want to do for different systems which means if we give name to the url and enter the name in any system the url should call from any system.

        Is it possible can give the suggesstion.

        Thanks in advance

        1. admin says:

          I’ve never tried running Apache over a server, however, you might want to check out the following sites for additional information:

          Can’t start Apache with mapped network drive – the solution seems to be to use UNC path instead of mapped drive.

          Alias network drive Apache Server – the solution is the same as above, use a UNC path

          Using Apache over a Local Network – the solution provided here is to edit your hosts file to map domain names to IP addresses (in this case, the IP address of the Apache Server.

          Accessing XAMPP Over a Network – here they recommend checking that Apache is listening to port 80, not 127.0.0.1:80. This requires modifying the httpd configuration files.

          Unable to connect Apache over LAN – some good network troubleshooting tips.

          I hope these give you something to work with.

          1. kishore says:

            actually i am trying to do url masking for this i m asking the doubt. thanks for your valuable suggestion.

            1. admin says:

              Oops, sorry about that.

              Perhaps these pages would be of use:

              URL Rewriting

              mod_rewrite: A Beginner’s Guide to URL Rewriting Article

          2. kishore says:

            hi,

            I have a doubt. Can i give my ip and application included while i change my domain name e.g: 11.21.34.125:8080/apex like this can i give in the host name?

            Please help me in this

            Thanks in advance

            1. admin says:

              There is no problem in assigning a URL a domain name.

              In the tutorial I map the website addressed by:

              localhost/joomla16

              to http://www.joomla.16

              You have to enter the appropriate information in the httpd-vhosts.conf file. See the section Getting Apache to Serve Your Web Site (steps 6 to 10).

          3. kishore says:

            Hi,
            can we able change the domain name with out tomcat or xampp.

            Thanks in advance

            1. admin says:

              I have never used Tomcat so I am not familiar with it. From what I’ve read about it, Tomcat seems to be a different web server.

              You change the domain name using the hosts file on your computer. For example, adding the line:

              127.0.0.1 www.google.com

              will cause all requests to http://www.google.com to be redirectd to your localhost server. But this is not enough. You need a localhost server in order to serve up web pages.

              The web server can be Apache, Tomcat, Microsoft IIS or something else. But you need to configure the web server as well. My instructions are for a system using Apache server (as bundled with XAMPP – XAMPP is just a prepackaged collection of Apache Server, MySQL database, PHP, pear, and some other utilities).

          4. kishore says:

            hi,

            Is it possible to give the name to my ip. for using the name can we able to access my system from another system.

            eg:
            my ip is 123.21.232.12 for this i wanna give a name like kishore.if i m trying to access my system application from another system we just mention the ip and port and application name there instead of ip i wanna use some name. Is it possible?

            if possible tell me the answer please.

            Thanks in advance

          5. kishore says:

            Hi,
            Can give me a answer for my question asked on 19 march

            1. admin says:

              Sorry, I thought my response from March 17 would point you in the right direction.

              You can definitely modify the hosts file on the remote computer so that when you type in mycoolsite.mine it redirects to the computer serving the site.

              In this case, you would modify the hosts file to point to the networked computer (you will have to find out the IP address of that computer).

              You will also, probably, have to modify the httpd-vhosts.conf on the server computer.

              This is about as much as I can help because I don’t have any networked computers.

  2. Subash says:

    thanks for great tutorial.now i got rid of that localhost.

    1. admin says:

      Awesome! Thanks for the feedback.

  3. tiwa says:

    hi

    in step3, I can’t save the changes in Notepad++,

    the error: please check whether if this file is oppened in another program

    1. admin says:

      That error message will occur if the file you are editting is locked. The most common reason is that you have openned the file with another application (for example,Word – which locks files when it opens them). It is necessary to close any applications that might have the file open.

      If worst comes to worse, you may ned to log out and log back in (not just switch user).

      UPDATE 17-March-2012: This error will occur on Windows 7 unless you are running the program with administrator privilege. See this article on how to do that.
  4. Viktor says:

    how about setting it up from apache 2

    1. admin says:

      It should work with Apache 2.x.

      The Apache Server found in XAMPP 1.7.3 is 2.2.14
      The Apache Server found in XAMPP 1.7.4 is 2.2.17
      The Apache Server found in XAMPP 1.7.7 is 2.2.21

      If you have installed Apache Server independantly of XAMPP, then some directories may be different, but the setup should be the same.

  5. Lei Raffaelli says:

    This is helpfull,,, thaks a lot, buddy.. :-))

    1. admin says:

      You’re welcome and thanks for the feedback.

  6. arlene says:

    sir please teach me how to configure my database settings in order to connect in the internet.. thanks a lot…

    1. admin says:

      I’ve never done it but my understanding is this:

      (1) you need a static IP address. Your computer (which is acting as your web server) must always be connected to the Internet at the same address. Most Internet conection plans give you a dynamic IP address – it changes regularily. You will need to ask your Internet provider for a static IP address. This will probably cost extra.

      (2) you need to point your domain name servers to your IP address. This way, when people type in mycoolsite.com the name server points them to your computer’s IP address on the Internet (now you see why you need a static IP address).

      (3) you need to ensure that port 80 (http) is open (not blocked by a firewall). If you need to serve secure pages, then you also need to ensure port 443 (https) is also open. If you also need ftp access, then you have to ensure that ports 20 (data) and 21 (command) are open.

      A quick search reveals that some companies (like this one) claim to allow you to run a site on a dynamic IP address as though the IP address was static. NOTE: I have no experience with this company or others offering the same service, so I cannot evaluate how true their claims are). Proceed at your own risk. From what I can see, they do this by having you prefix your site to a choice of domains they have (like mycoolsite.no-ip.org, or mycoolsite.servegame.com) andc with a bit of software magic, they redirect to your server (even if the IP address changes). I presume they are able to do this because you have to run some software on your server. I presume the software keeps their server informed of your IP address, so even if your IP address changes, they get updated about it.

      You can see which ports are open on your computer by using GRC’s port scanner. Click on Proceed then click on All Service Ports.

Leave a Reply

%d bloggers like this: