LAN access and virtual hosts - something i can't figure out

Problems with the Windows version of XAMPP, questions, comments, and anything related.

LAN access and virtual hosts - something i can't figure out

Postby mcbmcb0 » 19. May 2013 06:55

Hello
first post - i've read lots of threads on this and still confused: how to access virtual host names across a LAN using the server's ip or name?

I've upgraded to xampp 1.8.1 (was 1.7.7), control panel 3.1.0 on win 7 64 and have modified httpd-vhosts.conf and the hosts file to allow LAN access. works fine in that I can access the main xampp page across the LAN and my sites that are saved in c:\xampp\htdocs\...

I've wanted the document root for one site to be at the top of its folder (c:\xampp\htdocs\home\site1) to simplify my php paths (e.g $_SERVER['DOCUMENT_ROOT']) when uploading this to an online server. So I've also added a virtual host name for the folder as such:
Code: Select all
NameVirtualHost *:80

# other stuff....

<VirtualHost *:80>
   DocumentRoot "C:/xampp/htdocs/home/site1.local"
   ServerName site1.local
   ServerAlias www.site1.local
   <Directory "C:/xampp/htdocs/home/site1.local">
      Options Indexes FollowSymLinks Includes execCGI
      #xampp 1.7.7 format:      
      Order allow,deny
      Allow from all
               #xampp 1.8.1 format add:
      Require all granted
   </Directory>
</VirtualHost>

And in hosts added:
Code: Select all
127.0.0.1 site1.local


This all works well when accessing site1 on the same machine as xampp: e.g., http://site1.local -> loads fine.
BUT it fails when i try and access it across the LAN from other machines, e.g: 192.168.1.66/site1.local/ -> 404.
and while 192.168.1.66/home/site1.local/index.php loads, it breaks my 'includes' as the document root is now C:/xampp/htdocs (not C:/xampp/htdocs/home/site1.local).

I have checked httpd.conf where is set to 'Listen 80' and have tried 'Listen *:80' with no luck - i guess xampp is listening to all requests.

I've read various threads about modifying host files on each LAN machine but don't want to (there are macs, adroids,...) and am happy to type in the ip or network name. And I just don't understand why this doesn't work.

Can someone explain this for me, and preferably how to fix it.

thanks

Mike
mcbmcb0
 
Posts: 6
Joined: 19. May 2013 06:17
Operating System: win 7 64

Re: LAN access and virtual hosts - something i can't figure

Postby JJ_Tagy » 19. May 2013 18:51

Looks like you are confusing dns resolving, virtual hosts, and root folders.
JJ_Tagy
 
Posts: 788
Joined: 30. January 2012 13:44
XAMPP version: 5.5.15
Operating System: Windows 10 Pro x64

Re: LAN access and virtual hosts - something i can't figure

Postby mcbmcb0 » 20. May 2013 06:00

Looks like you are confusing dns resolving, virtual hosts, and root folders

yes i think i almost certainly am. can you clarify this for me, and how to resolve my problem?

thanks
mcbmcb0
 
Posts: 6
Joined: 19. May 2013 06:17
Operating System: win 7 64

Re: LAN access and virtual hosts - something i can't figure

Postby Nobbie » 20. May 2013 10:56

mcbmcb0 wrote:can you clarify this for me, and how to resolve my problem?


Simply declare all VirtualHosts for the physical LAN IP of the Server (i.e. 192.168.1.66) instead of 127.0.0.1 (replace 127.0.0.1 by 192.168.1.66 in the VirtualHost Definition).

Same for the Hosts files, replace 127,0.0.1 by 192.168.1.66 and finally edit ALL(!) hosts files (on every PC in your LAN) and apply site1.local

P.S.: I just realized that you defined VirtualHost on *:80 (not on 127.0.0.1), you may leave this as it is. Simply replace the hosts files as mentioned above.

P.P.S:
BUT it fails when i try and access it across the LAN from other machines, e.g: 192.168.1.66/site1.local/ -> 404.


This is nonsense in any way, you have to access the server by http://site1.local in any case

and while 192.168.1.66/home/site1.local/index.php loads.


And this is an ugly and unwanted side-effect due to the fact, that you placed the DocumentRoot of site1.local as a subfolder under the standard DocumentRoot C:/xampp/htdocs. I do not recommend "stacking" DocumentRoots, you *should" put the DocumentRoot of site1.local to C:/xampp/site1.local/htdocs (for example).
Nobbie
 
Posts: 13170
Joined: 09. March 2008 13:04

Re: LAN access and virtual hosts - something i can't figure

Postby mcbmcb0 » 20. May 2013 11:56

thank Nobbie for your reply. yes if i edit the other machine's hosts file all is fine, but i really would prefer not to do this on every LAN machine for every xampp site. my goal is:
  • a page accessible across the LAN which lists/links to all xampp sites including 'real' development sites and ones my kids are learning with (easy enough if under htdocs), and
  • each site has its document root in its own top level folder (possible with stacking...!), and
  • every time the list of sites changes i don't want to update all LAN host files (as the list will change quite often).
i'm not worried about the ugly addressing - i like ugly!

perhaps another way of phrasing my question is, is there a way to access XAMPP sites across a LAN where each has its document root in its own top level folder, and WITHOUT editing each LAN machine's hosts files.

clearly i know very little about servers, resolving dns, virtual hosts etc. all help greatly appreciated.
thanks
Mike
mcbmcb0
 
Posts: 6
Joined: 19. May 2013 06:17
Operating System: win 7 64

Re: LAN access and virtual hosts - something i can't figure

Postby Nobbie » 20. May 2013 14:45

mcbmcb0 wrote:is there a way to access XAMPP sites across a LAN where each has its document root in its own top level folder, and WITHOUT editing each LAN machine's hosts files.


It is by far the easiest solution for a small LAN to use the hosts file for the different site names. If you dont want to edit all the hosts, you have to set up a "DNS" Server instead - what is BY FAR more sophisticated and BY FAR more difficult. And it is an overkill for this small amount of work.

So in your case the answer is: No. You have to edit all the hosts files. The problem is, how can a client PC know, which IP is meant by site1.local (for example)? This has to be resovled BEFORE the request is executed. The hosts file is the most easiest way to tell TCPIP (and herewith your browser), which IP is assigned to site1.local. For the internet connecion this translation from Domain Name to IP is done by a DNS Server provided by your Provider. But on your local LAN it is your responsibility to introduce the IP to ALL PCs which will request from site1.local. If you dont do so, all request to site1.local will result in "server not found" ot similar.
Nobbie
 
Posts: 13170
Joined: 09. March 2008 13:04

Re: LAN access and virtual hosts - something i can't figure

Postby JJ_Tagy » 21. May 2013 00:20

Or use the specific IP address as you mentioned. Only problem is if you have more than one virutal server on the same machine, that won't work without editing the hosts or using dns like Nobbie said.
JJ_Tagy
 
Posts: 788
Joined: 30. January 2012 13:44
XAMPP version: 5.5.15
Operating System: Windows 10 Pro x64

Re: LAN access and virtual hosts - something i can't figure

Postby mcbmcb0 » 21. May 2013 09:53

ok thanks again Nobby and JJ-Tagy.
one last gasp at this if you can bear it...

without modifying host files, across the lan I can access a folder in C:/xampp/htdocs/home/site1 by specifying the server machine's i/p: e.g, 192.168.1.66/home/site1 - ugly but works. i like ugly. don't even need a virtual host.
the only problem is the document root is still C:/xampp/htdocs - i want it to be C:/xampp/htdocs/home/site1. yes that would be stacked but...?

So... is there any way an .htaccess file in the site1 folder can rewrite requests to any file in (or below) the site1 folder to have .../site1/ as its document root - without having to alter other htaccess or httpd.conf? (or hosts)

I tried and failed. I clearly don't understand rewrite rules - to paraphrase apache.org's rewriteguide I'm closer to shooting myself in the foot than feeling the power


thanks again

Mike
mcbmcb0
 
Posts: 6
Joined: 19. May 2013 06:17
Operating System: win 7 64

Re: LAN access and virtual hosts - something i can't figure

Postby Nobbie » 21. May 2013 11:15

mcbmcb0 wrote:So... is there any way an .htaccess file in the site1 folder can rewrite requests to any file in (or below) the site1 folder to have .../site1/ as its document root - without having to alter other htaccess or httpd.conf? (or hosts)


You can either insert a "Redirect" in a .htaccess (but "Redirect" can only Redirect to a valid URL), or use ALIAS in httpd.conf to define a different target for /site1. I recommend Alias, because Alias targets to any directory, whereas Redirect targets to a valid URL (this is a big difference). Examples:

In .htaccess

Redirect /home/site1 http://192.168.1.66/site1
(be careful to avoid endless loop!)


or (by far better in my mind) in httpd.conf:

Alias /site1 c:/whatever/you/want

This results any request from http://192.168.1.66/site1 is going to c:/whatever/you/want (i.e. http://192.168.1.66/site1/myfile.html results in c:/whatever/you/want/myfile.htm). You may define as many Alias as you want (Alias /site2 c:/somewhere/else ). YOU CANNOT "ALIAS" INSIDE .htaccess, this MUST BE in httpd.conf!!

If you do so, do not forget to grant sufficient rights to the target directories (c:/whatever/you/want, c:/somewhere/else). See the <Directory>-Block of the c:/xampp/htdocs for example.

Finally, if you dont want to adapt the hosts files, you CANNOT reach Xampp via http://site1.local, you MUST USE http://192.168.1.66 in any case (or the Windows Servername - this is known to the LAN without editing the hosts file): http://Servername (where Servername is the Windows Name of the Xampp PC).
Nobbie
 
Posts: 13170
Joined: 09. March 2008 13:04

Re: LAN access and virtual hosts - something i can't figure

Postby mcbmcb0 » 21. May 2013 11:40

thanks Nobbie - i'll give this a try tomorrow and report back

Mike
mcbmcb0
 
Posts: 6
Joined: 19. May 2013 06:17
Operating System: win 7 64

Re: LAN access and virtual hosts - something i can't figure

Postby mcbmcb0 » 23. May 2013 12:01

hello again
well i can redirect fine tho could not get the alias to work in httpd.conf - both
Code: Select all
Alias /site1_local/ C:xampp/htdocs/site1_local/
and
Code: Select all
Alias /site1_local/ http://site1.local/
do nothing (I slightly changed paths to htdocs/site1_local to distinguish from the virtualhost name site1.local). Can an alias point to a virtualhost? was i supposed to add a directory block?

my only half success was in htaccess:
Code: Select all
RewriteEngine On
RewriteCond  %{REQUEST_URI} ^(/home/site1_local)(/.*)
#RewriteRule  (.*)$       http://site1.local/$1 [L]

that will just redirect and, i think as the new location starts with http:// will always change the displayed url.

The problem remains that accessing across the LAN (e.g. 192.168.1.66/home/site1_local) breaks with the redirects, and messes up the php includes without them.
Is there anyway to not show the changed url? have tried [P] (-> 500 error) and a failed AliasMatch (in httpd.conf) all with no luck

failing that, its back to host files ( :( ) or rewrite my sites to find 'include' directories from a config file :cry:
all further help greatly appreciated!

Mike
mcbmcb0
 
Posts: 6
Joined: 19. May 2013 06:17
Operating System: win 7 64


Return to XAMPP for Windows

Who is online

Users browsing this forum: No registered users and 103 guests