Only apply virtual host when the server name is used as host - apache

So I have a project in folder htdocs/project. I need to access this project via domain project.xx to test subdomain mod_rewrite. To do this, I modified my hosts file to map the project.xx to 127.0.0.1.
Then, I have created a virtual host in apache using this code:
<VirtualHost *:80>
ServerName project.xx
ServerAlias www.project.xx
DocumentRoot C:/xampp/htdocs/project/
</VirtualHost>
It works as expected and when navigating to http://project.xx I end up on site running in C:/xampp/htdocs/project/.
I was just editing something else and I have discovered that something is wrong - navigating to 127.0.0.1 will also take me to my project site.
How do I prevent the virtual host setting from affecting whole localhost? I only want to go to my project when browser sends project.xx as the Host header.

As I learned on #httpd IRC chanell, it's required that you also set virtual host for the localhost itself, once you started using localhost.
This means that before matching the domain project.xx, a rule for 127.0.0.1 and localhost muse exist - so that if the domain isn't specified, default behavior will be used:
#Rule for localhost as it normaly behaves
<VirtualHost *:80>
#IP and `localhost` domain name
ServerName 127.0.0.1
ServerAlias localhost
#The normal document root
DocumentRoot C:/xampp/htdocs/
</VirtualHost>
#Rule for your project
<VirtualHost *:80>
ServerName project.xx
ServerAlias www.project.xx
DocumentRoot C:/xampp/htdocs/project/
</VirtualHost>

Related

Xampp unable to load multiple sites

I am working on a project using xampp server. I have more than one project. So for this I have set my httpd-vhosts like below
<VirtualHost *:8080>
ServerName khpos.com
ServerAlias www.khpos.com
DocumentRoot "D:/xampp/htdocs/pos"
<Directory "D:/xampp/htdocs/pos">
Require all granted
</Directory>
</VirtualHost>
#my second site
<VirtualHost *:8080>
ServerName demopos.com
ServerAlias www.demopos.com
DocumentRoot "D:/xampp/htdocs/demopos"
<Directory "D:/xampp/htdocs/demopos">
Require all granted
</Directory>
</VirtualHost>
Whenever I try to hit localhost:8080/demopos it's redirecting towards .../pos
hosts file
127.0.0.1:8080 khpos.com
127.0.0.1:8080 demopos.com
How to set it
Any help would be highly appreciated
You should change the portnumbers of the second etc. sites. Now all port addresses are on port 8080, if you change those to an other port number you should be fine.
<VirtualHost *:8080> to <VirtualHost *:{port-number}>
Where port-number is an other port-number
127.0.0.1:8080 khpos.com
127.0.0.1:{port-number} demopos.com
you have 2 virtual hosts defined and also the names in hosts file. Please first remove the port numbers in the hosts file:
ServerName khpos.com
ServerName demopos.com
127.0.0.1 khpos.com
127.0.0.1 demopos.com
if you call khpos.com:8080 or demopo.com:8080 it should reach your correct virtual hosts.
But: if you call localhost:8080 it will match none of your virtual hosts.
In that case Apache always enters the first virtual host matching the port that you have in your conf file ignoring its server name. That is a strange behavior of Apache - in my opinion a bug. It is exactly what you observe.
For that reason I always place a dummy virtual server in front of all others that can catch all non fitting requests with a dummy message in a simple html file.
Just a hint: you have defined
DocumentRoot "D:/xampp/htdocs/pos"
so there currently is no khpos.com:8080/pos because pos is part of the root unless you create another folder pos below

Using a hostname on Apache rather than an IP

I am wanting to run a hostname on apache (on xampp) and I have changed the httpd.conf file so that ServerName example.com.
I then changed httpd-vhosts.conf so that
<VirtualHost *:80>
ServerAdmin admin#example.com
DocumentRoot "C:/xampp/htdocs/example"
ServerName example.com
ErrorLog "logs/example.com.log"
CustomLog "logs/example.com.log" common
</VirtualHost>
and then added 127.0.0.1 example.com to the hosts file in the System32->drivers->etc folder.
It works on the computer running xampp (I think because that has the altered hosts file) but it will not work from other computers on the same network, which I gather is supposed to be handled by the virtual hosts file.

Laravel Virtual host doesnt work

Iam trying to make virtual host for laravel 5.2 in apache server, but everytime i tried to access the virtual host it always shows "This site cant be reached", but i can access it through localhost:folder/public
I already setting the httpd-vhost
<VirtualHost *:80>
DocumentRoot "C:/xampp2/htdocs/"
ServerName localhost
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "C:/xampp2/htdocs/belajarlaravel/public"
ServerName belajarlaravel.dev
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "C:/xampp2/htdocs/merchandise3-project/public"
ServerName merchandise.dev
</VirtualHost>
and my host
127.0.0.1 localhost
127.0.0.1 belajarlaravel.dev
127.0.0.1 merchandise.dev
I can access the localhost but for other host they still showing server not found and site cannot be reached. Can u give me some advice ? Thank You for your attention.
maybe the .dev ending is considered unsafe by Chrome, try .com

Is www included in the server name in apache virtual host

I'm looking to set up VirtualHosts on my apache server and was looking for documentation that would tell me if these 2 entries are identical
<VirtualHost *:80>
DocumentRoot /www/example1
ServerName www.example1.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /www/example1
ServerName example1.com
</VirtualHost>
Notice the lack of www. in the second one.
Thanks
This is from the Apache docs (http://httpd.apache.org/docs/2.0/mod/core.html#servername):
The ServerName directive sets the hostname and port that the server uses to identify itself. This is used when creating redirection URLs. For example, if the name of the machine hosting the web server is simple.example.com, but the machine also has the DNS alias www.example.com and you wish the web server to be so identified, the following directive should be used:
ServerName www.example.com:80
So I guess they are not identical.

Access Apache VirtualHost from any computer on LAN?

I have wamp setup with quite a few websites setup as virtual hosts like this in httpd.conf
<VirtualHost 127.0.0.1>
ServerName project1.local
DocumentRoot "c:/wamp/project1/"
</VirtualHost>
I have these input in the wamp machine's host file and I can access them just fine on that machine.
127.0.0.1 project1.local
However, when I try to put an entry on my OSX machine as (192.168.1.101 being the internal ip of the wamp machine) it won't pull the page up.
192.168.1.101 project1.local
Is there something else I need to do to make this work from other machines? Thanks!
You either need <VirtualHost 192.168.1.101> (in addition to 127.0.0.1), or simply use <VirtualHost *> to put the VH on all addresses.
Just add below code in your virtual host config file
In the below code,
'Client_IP' is the IP of the machine from where you want to access the directory without using any ip on the address bar, just put severname in the address bar like 'servername/'.
<VirtualHost *:80>
ServerName servername
DocumentRoot d:\wamp\www\dir_name
<Directory "d:\wamp\www\dir_name">
Order Allow,Deny
Allow from 127.0.0.1 Client_IP
</Directory>
</VirtualHost>
Then, set same servername that you have used for the virtual host on apache server like,
server_ip servername
in the client machine c:/windows/system32/drivers/etc/hosts file.