DocumentRoot "c:/wamp/www/" Se...">

Apache virtual hosts not working as expected

My Apache "httpd-vhosts.conf" looks like this:

<VirtualHost *:80>
    DocumentRoot "c:/wamp/www/"
    ServerName localhost
    ServerAlias *.localhost
</VirtualHost>

<VirtualHost laravel.dev:80>
    DocumentRoot "c:/wamp/www/laravel/public"
    ServerName laravel.dev
    ServerAlias *.laravel.dev
</VirtualHost>

<VirtualHost learninglaravel.dev:80>
    DocumentRoot "c:/wamp/www/learningLaravel/public"
    ServerName learningLaravel.dev
    ServerAlias *.learningLaravel.dev
</VirtualHost>

      

and my "... system32 / drivers / etc / hosts" also looks like this:

127.0.0.1       localhost
127.0.0.1       localhost

// I added the following entries. The first two entries above was already there
127.0.0.1       laravel.dev
127.0.0.1       learninglaravel.dev

      

When I enter "learningLaravel.dev" and "laravel.dev" into the browser, they work fine as expected. But I have other folders in my "www" folder where I use them to learn PHP, and I want to be able to access the files in those folders directly from the browser, for example say "localhost / test / me.php" . But every time I enter such an address, the browser navigates to the second entry in the vhosts-conf file [which prints a laravel error indicating that it cannot find the file]. It seems that the first entry in the vhosts-conf filedoesn't work and Apache bypasses it to the second entry. The first record will presumably capture all records. I tried swapping the second and third entries to see how it behaves, but it always directs the browser to the second entry, not the catch (first entry), which is supposed to handle addresses like "localhost / test / me .php "

Anytime I only enter "localhost" in the browser, it goes straight to the second entry, instead of printing the contents of the "www" folder . p>

How to solve this problem? thank.

+3


source to share


1 answer


The problem seems to be related to the way you are using the directive VirtualHost

.

Using a fully qualified domain name for the virtual host IP address is not recommended. It is misleading how it works. Name-based virtual hosts define the host using a directive ServerName

, not the fully qualified domain name in the VirtualHost

( <VirtualHost FQDN:80>

) directive . In fact it is seen as<VirtualHost 127.0.0.1:80>

What's going on, your document is documented in the VirtualHost doc , the last 2 paragraphs (just before "Security"), quoted:

When a request is received, the server first matches it with the best matching based on the local IP address and port combination only. Non-characters take precedence . If there is no match based on IP and port at all, the "main" server configuration b.

If multiple virtual hosts contain the best matching IP address and port, the server selects the best match from these virtual hosts based on the requested host name. If no name-based virtual host is found, then the first listed virtual host that matches the IP address will be used . As a consequence, the first virtual host listed for a given IP address and port combination is the default virtual host for this IP and port combination.

So, when you query localhost/somedir

, the server will try to find from VHosts declarations without wildcards, but not find them with matching hostname (ServerName), and so it selects the "default" first VHost with IP: port, not the first with *: port.



To solve your problem, try using <VirtualHost *:80>

vhost in all three declarations:

<VirtualHost *:80>
    DocumentRoot "c:/wamp/www/"
    ServerName localhost
    ServerAlias *.localhost
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot "c:/wamp/www/laravel/public"
    ServerName laravel.dev
    ServerAlias *.laravel.dev
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot "c:/wamp/www/learningLaravel/public"
    ServerName learningLaravel.dev
    ServerAlias *.learningLaravel.dev
</VirtualHost>

      

And restart / restart Apache.

(My only doubt is why Nasreddine can create a working test with your setup.)

+3


source







All Articles