3

I currently have this in my httpd.conf file in WAMP:

NameVirtualHost 127.0.0.1

<VirtualHost 127.0.0.1>
    ServerAlias *.dev.co.uk
    UseCanonicalName Off
    VirtualDocumentRoot D:/wamp/www/%1/httpdocs
</VirtualHost>

I created a directory "foo.bar" and then tried http://foo.bar.dev.co.uk and I get this:

Not Found

The requested URL / was not found on this server.

I want to get this setup working on my local apache server as well as my wamp server (I think the syntax is slightly different). If I need to give more info then leave a comment and I'll update.

udjamaflip
  • 682
  • 1
  • 8
  • 24
  • 1
    BTW, you might have gotten more responses had you posted this on http://webmasters.stackexchange.com/ or even http://serverfault.com/. I tried getting your question migrated but couldn't because of the bounty you placed. I hazarded an answer only because you weren't getting any responses after almost 2 weeks. – Shawn Chin Jun 20 '11 at 09:38

2 Answers2

2

I've never used VirtualDocumentRoot, but seeing that you're not getting any responses I'll give it a go.

According to the docs on Directory Name Interpolation, it would seem like your problem lies in your use of %1 which will match only "foo" for http://foo.bar.dev.co.uk.

To match "foo.bar", try:

VirtualDocumentRoot D:/wamp/www/%1.0.%2.0/httpdocs

%1 matches "foo" while %2 matches "bar". The addition .0 appended to each format is to allow us to include the dot in between foo and bar (since . is also a used as an operator to extract a substring of the matched words).

If that doesn't work, do check the logs to see which directory Apache is actually looking for. That may provide hints as to what has gone wrong.

Update

In response to:

"This does make foo.bar.dev.co.uk however, unfortunately it stops foo.dev.co.uk from working, any ideas?"

Using directory name interpolation to match aliases with indeterminate numbers of subdomains is tricky.

The most straight forward approach would be to simply use %0 and have the directories named after the full domain name.

VirtualDocumentRoot D:/wamp/www/%0/httpdocs

This will match with directories D:/wamp/www/foo.bar.dev.co.uk/httpdocs and D:/wamp/www/foo.dev.co.uk/httpdocs.

If you have a limited subset of possible domains, a more specific solution can be crafted. For example, if you have:

  • "foo.dev.co.uk"
  • "bar.dev.co.uk"
  • "foo.bar.dev.co.uk"
  • .. and more domains in the form "X.Y.dev.co.uk"

then you can handle "foo" and "bar" a preceding VirtualHost block before handling the other domains.

<VirtualHost 127.0.0.1>
    ServerAlias foo.dev.co.uk 
    ServerAlias bar.dev.co.uk
    UseCanonicalName Off
    VirtualDocumentRoot D:/wamp/www/%1/httpdocs
</VirtualHost>

<VirtualHost 127.0.0.1>
    ServerAlias *.dev.co.uk
    UseCanonicalName Off
    VirtualDocumentRoot D:/wamp/www/%1.0.%2.0/httpdocs
</VirtualHost>
Shawn Chin
  • 84,080
  • 19
  • 162
  • 191
  • This does make foo.bar.dev.co.uk however, unfortunately it stops foo.dev.co.uk from working, any ideas? The original error was: File does not exist: D:/wamp/www/foo The new error is: File does not exist: D:/wamp/www/foo.dev – udjamaflip Jun 14 '11 at 21:44
  • I've let it pass through to give you 50% of the reward, as you aided me towards finding a potential solution, however, the real solution is to use mod_rewrite within httpd.conf I think, I haven't quite got this finalised yet, if you know a way then I am more than happy to place your reply as the final answer. – udjamaflip Jun 19 '11 at 22:44
  • @udjamaflip Unless I've misunderstood your requirements, I don't see how `mod_rewrite` can help simplify the solution. Your title mentions "redirect", yet the question body talks about mapping hostnames to document roots. If you can give more details about you're trying to achieve, I'll try to help. – Shawn Chin Jun 20 '11 at 09:31
0

Some comments that might support :

1) to resolve your localhost ip
You have to get your browser able to find what's on 127.0.0.1. So, if not done, first edit the hosts file on your Windows OS machine C:\Windows\System32\drivers\etc\hosts :

127.0.0.1 foo.bar.dev.co.uk

Provided this is not possible to use wildcards in that host file. To do so, you shall install this.

2) port :
Make sure your server is listening to the http default port, eg do not forget to add this directive in your httpd.conf file (as I did not see it) :

Listen 80

3) Control :
As you're using the NameVirtualHost XXX.XXX.XXX.XXX directive, make sure the is exactly matching the NameVirtualHost directive. It looks like this is the case on your post.

4) Path :
That's maybe a stupid remark from my side, as a Linux user, but I thought Windows OS requires backslashes for the path, and not slashes. But I might be definitely wrong there and would not like to mislead anybody. But as "all the directives in this Directory Name Interpolation module interpolate a string into a pathname, I would check the result of The obtained interpolated string. Pls see next point.

5) Placeholders :
Make sure the module vhost alias is loaded into your wamp. I would also recommend you to add a directive in your vhost sothat you can check in the logs what's the required interpolated URL, find what's wrong, and correct accordingly your apache setup :

LogFormat "%h %l %u %t \"%r\" %>s %b" common
CustomLog logs/access_log common 

6) Existing solution...
An existing problem has always existing solutions : See there

Once you'll have checked these points, I guess the solution would appear soon.

Community
  • 1
  • 1
hornetbzz
  • 9,188
  • 5
  • 36
  • 53