Windows Fake URL test [hosts file]

Published Sunday, March 09, 2008 5:33 AM

you can make your application respond with a different behavior based on requested URL, This can be as simple as showing a different logo or as sophisticated as filtering data by that requested domain

this is not such a big implementation problem, but it seems hard to test that is if you don't know how to fake that URL request on your localhost

well

there's a file in this path on a windows environment

%windir%\system32\drivers\etc\hosts

this file contains the definition of the keyword "localhost"

   1: # Copyright (c) 1993-1999 Microsoft Corp.
   2: #
   3: # This is a sample HOSTS file used by Microsoft TCP/IP  for Windows.
   4: #
   5: # This file contains the mappings of IP addresses to  host names. Each
   6: # entry should be kept on an individual line. The IP  address should
   7: # be placed in the first column followed by the  corresponding host name.
   8: # The IP address and the host name should be separated  by at least one
   9: # space.
  10: #
  11: # Additionally, comments (such as these) may be  inserted on individual
  12: # lines or following the machine name denoted by a '#'  symbol.
  13: #
  14: # For example:
  15: #
  16: #      102.54.94.97     rhino.acme.com          #  source server
  17: #       38.25.63.10     x.acme.com              # x  client host
  18:  
  19: 127.0.0.1       localhost

 

this is the original contents of the file note in line 19 there's a record that states that 127.0.0.1 (which is the loopback address,, always pointing to your machine) has the alias localhost

 

so all you need to do to test a URL is add a record that states that the same IP is also your domain,

and also for subdomains you can still do the same, i'd recommend adding the subdomains before the domain itself

like this

 

   1: 127.0.0.1    mail.domain.com
   2: 127.0.0.1    user.domain.com
   3: 127.0.0.1    www.domain.com
   4: 127.0.0.1    domain.com

 

now when you browse to http://domain.com or http://user.domain.com it'll issue a request to your local Web server and using code you can redirect or display conditional content on the default website

ah.. don't forget to restart your browser after editing and also you need to be an administrator to edit this file

 

Filed under:

Comments

# Zac Boyles said on Friday, June 13, 2008 12:00 PM

Just searching around for an example to get hosts file edits to work with sql's integrated security. If I reference the "Fake URL" in the connection string and test it, an error message displays stating that user "" cannot be validated. Just wondering why it doesn't carry it over.

If you know, please email zac.boyles@sunfestcaribe.com

Thanks,

Zac

# Rimon Tadros said on Saturday, September 20, 2008 10:02 PM

Good artical Amir,

Zac, you can connect to SQL server using many protocols, in SQL server 2005 you can use: TCP/IP, named pipes, shared memory, and VIA.

if you want to connect to SQL server use the hosts file' entry then your SQL server should be TCP/IP enabled

in SQL server 2005: go to "SQL Server Configuration Manager" and select "Network Configuration" and enable the TCP/IP

in SQL server 2000: go to “Client Network Utility” and in “SQL Server Client Network Utility”  window, click the “General” tab, and enable the TCP/IP

then from “services” restart SQL server, and connect to the entry in the hosts file, it should work :)

Leave a Comment

(required) 
(required) 
(optional)
(required) 

This Blog

Syndication