Subdomain cannot load css/images from main site

Status
Not open for further replies.

cyberfrag1149

New Member
Messages
2
Reaction score
0
Points
0
As an example, if my main site were http://example.com, and I made a subdomain called http://sub.example.com, how would I have the subdomain's pages load the CSS, PHP, or images from my main site? While it would be easiest to just duplicate each of these things, I'm trying to avoid that.

The subdomain's files are located in a folder in my public_html directory. I've tried changing the links to call site resources to something like ../images, or ../style, but that doesn't seem to work.

How can I do this?
 

essellar

Community Advocate
Community Support
Messages
3,295
Reaction score
227
Points
63
The dot-dot (parent) directory can't work in a browser that way, since the base "directory" is the site root (in this case sub.example.com). There is no directory "above" the site root; if the browser tried to construct a URL based on the relative URL "../images/image.jpg" while sitting at the URL "http://sub.example.com", it would result in "http://images/image.jpg". Remember, the browser can't see the server's file system. As far as it's concerned, the server is as "up" as you can go. The fact that the "server", in this case, is in reality a directory that lives below another "server" is immaterial.

You would need to use an absolute (or protocol-relative, to be safest) URL to get the images from that "other server". (A protocol-relative URL starts with two slashes, which tells the browser to use HTTPS if it's using HTTPS for the main page, or HTTP if that's what it's already using. So "//example.com/images/image.jpg" will not result in a "mix of secure and insecure content" warning.)
 

cyberfrag1149

New Member
Messages
2
Reaction score
0
Points
0
I tried doing that. I believe it works just fine for images, but it utterly broke the PHP. My error log is now full of stuff like:

Code:
[09-May-2013 23:16:10 America/Montreal] PHP Warning:  require() [<a href='function.require'>function.require</a>]: http:// wrapper is disabled in the server configuration by allow_url_include=0 in /home/maynos/public_html/sub/index.php on line 1
[09-May-2013 23:16:10 America/Montreal] PHP Warning:  require(http://flawedspirit.elementfx.com/includes/header.php) [<a href='function.require'>function.require</a>]: failed to open stream: no suitable wrapper could be found in /home/maynos/public_html/sub/index.php on line 1
[09-May-2013 23:16:10 America/Montreal] PHP Fatal error:  require() [<a href='function.require'>function.require</a>]: Failed opening required 'http://flawedspirit.elementfx.com/includes/header.php' (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/maynos/public_html/sub/index.php on line 1

From what I can gather, the server's doesn't do PHP require() calls with explicit URLs? If I'm reading this correctly?
Would require_once() or (worse yet) include() functions work?

EDIT: You snuck that post in while I was editing. Hmm, so are you saying to replace URL calls in my PHP functions with the "//example.com/whatever" format? Which is basically just removing the protocol from the URL? Tried that, just get more warnings and errors about how such PHP files don't exist in the subdomain (of course they don't). People make subdomains all the time. How in the world did they get around this without making duplicates of everything?
 
Last edited:

hbazer

Member
Messages
398
Reaction score
7
Points
18
With [ allow_url_include ] disabled, this method will not work:
PHP:
<?php include("http://example.com/includes/example_include.php"); ?>

Instead, the file must be included with a local path, and there are three methods of doing this:

1.> By using a relative path, such as
Code:
../includes/example_include.php

2.> By using an absolute path (also known as relative-from-root), such as
Code:
/home/username/example.com/includes/example_include.php

3.> By using the PHP environment variable $_SERVER['DOCUMENT_ROOT'], which returns the absolute path to the web root directory. This is by far the best (and most portable) solution.
PHP:
<?php include($_SERVER['DOCUMENT_ROOT']."/includes/example_include.php"); ?>

##########
You cannot pass a query string using the alternative solutions. You define the variables locally before performing the include:

To achieve the effect of this:
PHP:
<?php include("http://example.com/includes/example_include.php?var=example"); ?>

You must instead use this:
PHP:
<?php
$var = "example";
include($_SERVER['DOCUMENT_ROOT']."/includes/example_include.php");
?>
 

essellar

Community Advocate
Community Support
Messages
3,295
Reaction score
227
Points
63
The key to it all is knowing what the browser will have to ask for (images, CSS, JavaScript, frame sources, Ajax data, anchor URLs) and what the server will have to ask for (files that are included before the finished page is sent to the browser). Your include() and require() statements never make it to the browser.
 
Status
Not open for further replies.
Top