Friday, March 5, 2010

domains and subdomains on apache localhost windows

Its frustrating when you need to test how htaccess is going to work against domains and subdomains on your windows localhost (apache-mysql-php), when all you have to work with is a uri like:
http://127.0.0.1/etc or
http://localhost/etc
On windows you can create domains and subdomains to show up instead.


You need to alter 2 files:
  • httpd.conf [under your apache install, in the conf folder] and
  • hosts [in windows go to windows > system32 > drivers > etc]

To begin with, stop the Apache service. Otherwise it won't pick up on the changes til ur next restart. When you've done all the changes, then turn it back on.

httpd.conf

Open up httpd.conf in a decent text editor, or Notepad if you have nothing better. At the bottom of that file add the following:

NameVirtualHost *:80

<virtualhost *:80>
DocumentRoot "C:/path/to/documentroot"
ServerName localhost
</virtualhost>

<virtualhost *:80>
DocumentRoot "C:/path/to/documentroot/subfolder"
ServerName example.tld
ServerAlias www.example.tld www subdomain.example.tld subdomain
</virtualhost>

hosts file


Again, open this up in a decent text editor and type the following, being sure to keep each entry on a new line:

127.0.0.1       localhost
127.0.0.1       example.tld
127.0.0.1       www.example.tld
127.0.0.1       subdomain.example.tld

So what we have now is that instead of http://127.0.0.1 you can use http://localhost. This will work for anything on the documentRoot. (If you're not sure where your documentRoot is, check it in httpd.conf, it'll tell you).

You can now also use http://example.tld, http://www.example.tld and http://subdomain.example.tld in the subfolder that you specified here:
C:/path/to/documentroot/subfolder

It's handy when you have a lot of sites for testing in localhost under windows-apache-php-mysql and you need to see what's happening when testing .htaccess

No comments:

Post a Comment