0

I have been playing around with the windows HOSTS file which seems to be located under c:\windows\ (Windows 95, 98 y Me), and under C\Windows\system32\drivers\etc\ in (Windows NT, 2000, XP and Vista)

I would like to point

http://mydomain/ to a particular folder of my web server. I Hosts file currently looks like:

127.0.0.1       domainA
127.0.0.1       domainB

But what I would like is

http://domainA/ points to http://127.0.0.1/domainA/
http://domainB/ points to http://127.0.0.1/domainB/

How can I achieve this?

Tests

To use the rewriteModule uncomment the next line in your httpd.conf file:

LoadModule rewrite_module modules/mod_rewrite.so

The next code does not what I was looking for.

RewriteEngine  on
RewriteCond %{HTTP_HOST}   ^domainA [NC]
RewriteRule ^/(.*)         http://127.0.0.1/domainA/$1 [L,R]

With the above code when you type in your browser http://domainA/ it automatically changes it to http://127.0.0.1/domainA/ but what I want it to stay in http://domainA/ while serving the content of the http://127.0.0.1/domainA/ folder.

eg. http://127.0.0.1/ points to /htdocs

I want http://domainA/ to point to /htdocs/domainA

Solution

Add to apache httpd.conf the next lines:

<VirtualHost *:80>
ServerName http://domainA/
DocumentRoot "/htdocs/domainA"
</VirtualHost>

<VirtualHost *:80>
ServerName http://domainB/
DocumentRoot "/htdocs/domainB"
</VirtualHost>

3 Answers 3

3

Second answer :

You must use apache virtualhost capabilities :

<VirtualHost *:80>
# url like http://127.0.0.1/.....
ServerName 127.0.0.1
DocumentRoot /htdocs
</VirtualHost>

<VirtualHost *:80>
# url like http://domaina/.....
ServerName domaina
DocumentRoot /htdocs/domaina
</VirtualHost>

vhosts docs

2
  • Exactly what I was suggesting for IIS; +1 because I don't know Apache and you filled the gap.
    – Massimo
    Aug 26, 2009 at 13:35
  • This is what I did recently on an install of WAMP.
    – Hondalex
    Aug 26, 2009 at 21:53
2

The only purpose of the HOSTS file is mapping names to IP addresses; it doesn't have anything at all to do with URLS (the same is true for DNS). You just can't do that this way.

Other than rewriting URLs as suggested, you could also use host headers:

  • Define the two names in the HOSTS file and make them both point to 127.0.0.1
  • Define two websites in your web server, one which answers to "domainA" and the other one which answers to "domainB"

This way, when you call http://domainA, you will see the first web site; when you call http://domainB, the second one will show up; the web server will automatically recognize which name it's being called with and serve the appropriate web site, even if both are running on the same IP address.

If you want to use IIS, you need to be running a server version of Windows in order to create multiple web sites; IIS on client systems like XP and Vista only supports a single site.

2
  • host headers is a far simpler option than the rewriting route, although if you are stuck with using apache you don't have much choice other than to use a re-write
    – Jim B
    Aug 26, 2009 at 11:32
  • Why, can't Apache serve multiple web sites based on host headers?
    – Massimo
    Aug 26, 2009 at 11:53
2

You talk about two differents thinks :

  1. name resolution (dns, hosts file)
  2. url rewriting (transform an url)

you can't do url writing with your hosts file.

to do :

 http://domainA/ points to http://127.0.0.1/domainA

You need use an url writing api on you web server :

or

Sample configuration for apache :

RewriteEngine  on
RewriteCond %{HTTP_HOST}   ^domainA [NC]
RewriteRule ^/(.*)         http://127.0.0.1/domainA/$1 [L,R]


RewriteCond %{HTTP_HOST}   ^domainB [NC]
RewriteRule ^/(.*)         http://127.0.0.1/domainB/$1 [L,R]
3
  • I am using xampp. So it is apache. Any quick tip? Aug 26, 2009 at 11:08
  • I have rewritten my post to explain my problem better. Thanks for your help. Aug 26, 2009 at 11:56
  • 1
    Add the apache tag to you question, you could have more answer :)
    – Gayell
    Aug 26, 2009 at 13:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .