InfoHeap
Tech tutorials, tips, tools and more
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

Networking tutorials

  • DIG quick start tutorial
  • Dig - list all dns records
  • How to view wifi connection speed on Mac
  • Linux iptables - Nat port forwarding using PREROUTING
  • Linux iptables and ip6tables examples
  • Ngrep - quick start guide
  • Use nc to check if a remote port is reachable
  • Use nc to listen to tcp or udp port
  • Use nc to test HTTP url redirection
  • add more DNS servers to Wi-Fi interface on Mac
  • locally override website host to IP mapping
  • use nc to print headers from browser
 
  • Home
  • > Tutorials
  • > Networking

How to locally override website domain (or hostname) to IP mapping

By admin | Last updated on Dec 8, 2015

When we browse a web site, the browser first do a DNS lookup to find the IP address of the domain name. We can override this IP by an IP of our choice so that browser hits the IP we want instead of the IP obtained from DNS server. Now why would someone want to override the IP mapped to a specific domain? Here are some reasons:

  1. As a developer you may want to hit a specific server to debug. If you have multiple server behind a load balancer or multiple aname records for a domain name, you may hit any of the IP addresses. Overriding it lets you hit a specific server.
  2. If you are migrating your site, you may want to set it up on a new ip address first and then change the mapping locally to test the new site before permanently migrating.

Steps on Mac or Linux using hosts file

  1. Edit /etc/hosts file using sudo vi /etc/hosts and add this entry:
    aaa.bbb.ccc.ddd   www.yourhostname.com

    Here replace aaa.bbb.ccc.ddd with the desired IP address. You can also put multiple host entries separated by space.

  2. Now ping www.yourhostname.com (or whatever domain name you have setup) using:
    ping  www.yourhostname.com

    This should show some like this:

    PING www.yourhostname.com (aaa.bbb.ccc.ddd): 56 data bytes
    ...
    ...

    Ensure that the ip address matches with what you entered in the hosts file.

  3. If you are running a web server at the ip address mentioned in the hosts file,  then access http://www.yourhostname.com/ in your browser and you will hit the desired ip address.

Steps on Windows 7 using hosts file

These are the instructions for window 7. For other window versions steps may be slightly different.

  1. We can modify hosts file using notepad running as administrator. Open Notepad as administrator by right clicking on Notepad program and selecting “Run as administrator” as shown below:
    windows-notepad-run-as-administrator
  2. Edit the hosts file using notepad. The file location is c:\windows\System32\drivers\etc\hosts
    windows-hosts-file-pathMake sure that “All Files” is selected instead of “text documents” as this file does not have .txt extension.
  3. Edit the file and put desired ip address and domain name (hostname) as mentioned above. e.g.
    aaa.bbb.ccc.ddd www.yourhostname.com

    While saving ensure that it is saved as hosts file and not hosts.txt. To do this make sure “All files” is selected instead of “Text Documents”. Otherwise windows may put a .txt extension automatically while saving. If that happens override won’t work.

  4. Ping the hostname to verify if www.yourhostname.com points to correct entry. You may have to enable the command line utility ping on windows in case it is not enabled.

Suggested posts:

  1. How to run a program as administrator on windows 10
  2. How to migrate your site from one domain to another
  3. DIG quick start tutorial for DNS Lookup
  4. jQuery jsonp and cross domain ajax
  5. Handle XSS restriction using different domain for user entered javascript
  6. How to select a domain name for your site
  7. How to use nc (netcat) to print headers sent by a browser
  8. How to set up google analytics profiles for domain and sub-domain tracking
Share this article: share on facebook share on linkedin tweet this submit to reddit
Posted in Tutorials | Tagged Networking, Tutorials, Web Development

Follow InfoHeap

facebook
twitter
googleplus
  • Browse site
  • Article Topics
  • Article archives
  • Recent Articles
  • Contact Us
  • Omoney
Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries

Copyright © 2023 InfoHeap.

Powered by WordPress