setting up subdomains in order to do root based paths

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

setting up subdomains in order to do root based paths

Postby jrinco11 » 11. April 2009 22:53

hey guys - so I've been having serious issues trying to get subdomains to work with my windows xampp installation so I can use root based paths ("\includes\header.php") as opposed to ("includes\header.php"), because using relative paths won't always work out for me (I have multiple projects, and trying to use relative paths just don't work in some situations).

My xampp installation is in "C:\users\maki\docs\xampp"

my 'public folder' is at "C:\users\maki\docs\xampp\htdocs"

and the different 'sites' I want to use as subdomains (http://site1.localhost, http://site2.localhost, etc) are also in htdocs:
C:\users\maki\docs\xampp\htdocs\site1
C:\users\maki\docs\xampp\htdocs\site2
etc..

I have followed the guide here: viewtopic.php?p=92407 to no avail.

I uncommented this line in httpd.conf:
Code: Select all
# Virtual hosts
Include conf/extra/httpd-vhosts.conf


and have these lines in httpd-vhosts.conf:
Code: Select all
# Default site: http://localhost
<VirtualHost *:80>
DocumentRoot "C:/users/maki/docs/xampp/htdocs/"
ServerName localhost
ServerAlias localhost
</VirtualHost>

# Foo sub domain: http://site1.localhost
<VirtualHost *:80>
ServerName site1.localhost
DocumentRoot "C:/users/maki/docs/xampp/htdocs/site1/"
DirectoryIndex index.php index.html index.html index.htm index.shtml
</VirtualHost>


now, for site1, if I have "C:/users/maki/docs/xampp/htdocs/site1/index.php" and "C:/users/maki/docs/xampp/htdocs/site1/includes/header.php":

index.php:
Code: Select all
<p><?php echo "My location: " . dirname(__FILE__); ?></p>

<?php
include("\includes\header.php");
echo 'this is the index - realpath:' . realpath("\includes\header.php");
?>


this is what I get when I browse to http://site1.localhost:
Code: Select all
My location: C:\users\maki\docs\xampp\htdocs\site1

Warning: include(\includes\header.php) [function.include]: failed to open stream: No such file or directory in C:\users\maki\docs\xampp\htdocs\site1\index.php on line 5

Warning: include() [function.include]: Failed opening '\includes\header.php' for inclusion (include_path='.;C:\users\maki\docs\xampp\php\pear\') in C:\users\maki\docs\xampp\htdocs\site1\index.php on line 5
this is the index - realpath:


HOWEVER, If I remove the "\" from the paths in the include statements, it works fine.....

I am about to just have to have each project in it's own xampp installation (have each site's contents in the root of htdocs), but I'd rather not have to have 200+mb for each site (because of the size of xampp) -- any suggestions at all???

thanks!
jrinco11
 
Posts: 4
Joined: 11. April 2009 22:36

Re: setting up subdomains in order to do root based paths

Postby Wiedmann » 11. April 2009 23:13

include() or realpath() are filesystem functions and are working with filesystem paths, and not with URIs.
Thus:
so I can use root based paths ("\includes\header.php")

is really from the root of your filesystem (on the current drive, because the drive letter is missing), and not from some "viratual root" which Apache is using to build URIs (the DocumentRoot).

But if you are using PHP on Apache, you can use the Variable $_SERVER['DOCUMENT_ROOT'] to build paths, starting from the DocumentRoot of the current VirtualHost.

HOWEVER, If I remove the "\" from the paths in the include statements, it works fine.....

And what's the problem? If you use relative paths, you can put your page where ever you want (in htdocs, or a subdirectory), and it's always working. With fixed paths, starting from your filesystem root, or the DocumentRoot, you must have the page always at the same place.
Wiedmann
AF Moderator
 
Posts: 17106
Joined: 01. February 2004 12:38
Location: Stuttgart / Germany

Re: setting up subdomains in order to do root based paths

Postby jrinco11 » 12. April 2009 00:33

ah, thanks for clearing that up!

And what's the problem? If you use relative paths, you can put your page where ever you want (in htdocs, or a subdirectory), and it's always working. With fixed paths, starting from your filesystem root, or the DocumentRoot, you must have the page always at the same place.


normally, it wouldn't be an issue, until I created a setup for friendly urls :)

right now, at the root of site1, my .htaccess is as follows:
Code: Select all
<Files ~ "creations">
ForceType application/x-httpd-php
</Files>


this allows me to create a friendly URL so people can go to http://site1.localhost/creations/category-id

however, when visiting that URL, and using "include('includes\header.php');", it's searching for it at "site1.localhost/creations/includes/header.php", as opposed to "site1.localhost/includes/header.php" like I need it to.

Perhaps I'm going about this the wrong way? any suggestions?
jrinco11
 
Posts: 4
Joined: 11. April 2009 22:36

Re: setting up subdomains in order to do root based paths

Postby Wiedmann » 12. April 2009 01:02

however, when visiting that URL, and using "include('includes\header.php');", it's searching for it at "site1.localhost/creations/includes/header.php",

No. As I've posted above, include() is not working with URIs and does not know anything about settings in a ".htaccess".
--> The location from the file creations doesn't change in the filesystem because of this ".htaccess". Thus a path, relative to the filesystem location of this file, is always the same.
Wiedmann
AF Moderator
 
Posts: 17106
Joined: 01. February 2004 12:38
Location: Stuttgart / Germany

Re: setting up subdomains in order to do root based paths

Postby jrinco11 » 12. April 2009 13:49

thanks for all your help, Wiedmann!

However, I think when I said "however, when visiting that URL, and using "include('includes\header.php');", it's searching for it at "site1.localhost/creations/includes/header.php"," you may have thought that was a question - but it was a statement :)

take the following url for example, "http://site1.localhost/creations/creation123", to which the "local file" that is being accessed for this url is actually "htdocs/site1/creations" (which is just a php [text] file, used for creating the friendly urls I mentioned earlier).

within the file "creations", I have this php include, which works fine - and the realpath output does indeed show "C:\users\maki\docs\xampp\htdocs\site1\includes\session.php " as it should:
Code: Select all
<?php include_once('header.php'); echo realpath("header.php");?>


however, within header.php, I am trying to place this image on the page:
Code: Select all
<a href="index.php"><img src="images/logo_site1.png" alt="site1.com logo"  width="370" height="88"></a>


but the image is not loading - within firefox, I "copy image url" and I get this: localhost/spiffycake/creations/images/logo_site1.png -- when it should be localhost/site1/images/logo_site1.png. HOWEVER, if I am browsing site1.localhost/creations/index.php, all the images and such load just fine.

sorry to keep bugging you, but any more suggestions?? :)
jrinco11
 
Posts: 4
Joined: 11. April 2009 22:36

Re: setting up subdomains in order to do root based paths

Postby Wiedmann » 12. April 2009 16:26

However, I think when I said "however, when visiting that URL, and using "include('includes\header.php');", it's searching for it at "site1.localhost/creations/includes/header.php"," you may have thought that was a question - but it was a statemen

I know that's a statement. But this statement is wrong:
"include('includes\header.php);" is searching for the file at "C:\users\maki\docs\xampp\htdocs\site1\includes\header.php" (filesystem path) and not at "site1.localhost/creations/includes/header.php" (URI path)

but any more suggestions??

You are mixing two different things:

Code: Select all
<?php include_once('header.php'); echo realpath("header.php");?>

include() is working with filesystem path. (relative to the current script directory, or absolute from the top of your harddrive.

In your example, PHP is searching the file at: "C:\users\maki\docs\xampp\htdocs\site1\header.php", regardless if you request this file in your browser with "http://site1.com/creations.php" or with "http://site1.com/creations/category-id".


Code: Select all
<a href="index.php"><img src="images/logo_site1.png" alt="site1.com logo"  width="370" height="88"></a>

A link in a html code is an URI and your browser accessing your server with this URI. A relative URI is relative to the current URI-path (and resource). With "normal" URIs, relative URI's are relative to the current document you request. With your friendly URI's, relative URIs must not be relative to the current document you request. And absolute URI is starting at the DocumentRoot (in the filesystem).

- If you request the document with this link with "http://site1.com/creations.php", your browser is reqesting "http://site1.com/images/logo_site1.png" and Apache is searching this file at "C:\users\maki\docs\xampp\htdocs\site1\images\logo_site1.png".

- If you request the document with this link with "http://site1.com/creations/category-id", your browser is reqesting "http://site1.com/creations/images/logo_site1.png" and Apache is searching this file at "C:\users\maki\docs\xampp\htdocs\site1\creations\images\logo_site1.png".


Well, so the main question:
Did you have a problem with your PHP includes, or your links in HTML?
Wiedmann
AF Moderator
 
Posts: 17106
Joined: 01. February 2004 12:38
Location: Stuttgart / Germany

Re: setting up subdomains in order to do root based paths

Postby jrinco11 » 15. April 2009 02:54

ah, I think understand what you are telling me know...

when doing php includes, the paths used there are as you said 'against' the filesystem, so they are going to be relative to "where the current file is on the server", so, if I have a file "index.php" with an include('aFile.php'), it will be looking at the same level directory that "index.php" is located at, and if I were to do include("/aFile.php"), it will look for that file at the root of what ever drive "index.php" is located on (in my case, c:\).

whereas, when I do any URI "paths" (such as creating links, etc), it will basically "based" on the URI itself -- so if I am at localhost/index.php and I have a link in that file to "contactus.php", it will indeed link to localhost/contactus.php. However, if I am at localhost/creations/23 regardless if it is a friendly url or not, it will be searchign for contactus.php at localhost/creations/contactus.php.

correct?
jrinco11
 
Posts: 4
Joined: 11. April 2009 22:36

Re: setting up subdomains in order to do root based paths

Postby Wiedmann » 15. April 2009 09:17

ah, I think understand what you are telling me know... correct?

Absolutely :-)
Wiedmann
AF Moderator
 
Posts: 17106
Joined: 01. February 2004 12:38
Location: Stuttgart / Germany


Return to XAMPP for Windows

Who is online

Users browsing this forum: No registered users and 76 guests