Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

How to Access the Site using Domain Name Instead of localhost in IIS

0.00/5 (No votes)
7 Oct 2014 1  
How to access the site using domain name instead of localhost in IIS

Introduction

Whenever we host any website in IIS (Internet Information Services), we used to access the website with localhost or with that particular machine IP address in the way of http://localhost/TestSite/TestPage.aspx.

Did you ever think of accessing your website with a domain name as http://www.testsite.com instead of http://localhost/testsite or http://127.0.0.1/testsite on your local machine?

How can I tell my IIS that http://www.testsite.com is pointing to the files on my local computer not try to access the internet?

The answer behind all these questions is Hosts file.

This will be in <Windows Root Folder>\System32\Drivers\etc\. In general, for the people who used to install Windows Operating System in C drive will be C:\Windows\System32\drivers\etc. You can open this file in Notepad, Notepad++ or any text editor that you have. If you open this file, it will be as follows:

Note: You need administrator privileges to save your changes in this file.

Case 1

If you want to create a new website which is possible for accessing through domain name, follow the below steps:

  1. Open IIS (Click WIN+R, enter inetmgr in the dialog, and click OK. Alternatively,  search for IIS Manger in start window).
  2. Expand the Server node and click on Sites folder.
  3. Click on Add Website in Actions pane.

    Note: If you need any help for the above steps, then please check out my blog post - How to setup basic website in iis 8 .

  4. Enter the details in the Add Website window as follows:

  5. Click on Ok to create website.

     

  6. If you try to browse your website now, you will see that your webpage is not available alert in Chrome. You will see the same kind of issue in other browsers also.

    This is because the address you entered is going to search in the internet instead of your localhost. To overcome this, open the Hosts file in any text editor and make the following changes:

    (add " 127.0.0.1 www.testsite.com " to hosts file)

    Now try to reload the page by clearing the browser cache. It will work as follows:

Case 2

If you want to access the website using domain name which is created already, then follow the below steps.

  1. Open IIS (Click WIN+R, enter inetmgr in the dialog, and click OK. Alternatively,  search for IIS Manger in start window).
  2. Expand the Server node and then expand Sites folder.
  3. Click on Website that you want to access using domain name and then click on Bindings in the Actions pane.

  4. Select the binding of type http and then click on Edit. This will open a new window as follows:

    Enter the host name in the provided text box. I am entering this as www.google.com because I want to access my site with Google address.

  5. Now make the change in hosts file as we did in 6 in case 1.

  6. Once you have made this change, you can access your local website with google address.

    Note: The changes that you are making in hosts files are applicable to that particular local machine in which that file exists.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here