Question

So I've never seen this before. I have an EC2 server (first time setting this up) using Debian Linux and Apache 2.2. Using a path like so /js/file.js is looking for http://js/file.js. On my local machine and my dreamhost shared server I don't have this problem. In fact I've never seen this problem on a server before. What it should do is look for http://domain.com/js/file.js. Does anyone have some idea of why this could be happening? I've poured over my php.ini file and don't have any hint at what I should change or add to fix this.

Was it helpful?

Solution 2

Ok, so I figured it out. The problem was that on my local machine and on my other server I was not at the root domain so I was using $_SERVER['SCRIPT_NAME'].DIRECTORY_SEPARATOR."js/" to determine the root url to make an alias to the js files path. This returned /app/js/. This worked fine when the files where in a sub folder from the domain somewhere e.g. domain.com/app/js/file.js.

However once I was installing this app on my server and it was the root application $_SERVER['SCRIPT_NAME'].DIRECTORY_SEPARATOR."js/" was returning //js/. That was the problem! That is not the same as /js/. That was bypassing the domain as part of the url and instead telling the browser to look at http://js like it was a fully qualified url. I am assuming // is shorthand for http:// though I've never tried this before.

To fix this I hacked together this function based off of something in the Yii Framework, which is what I'm using for this application.

function getBaseUrl() {
    $scriptName=basename($_SERVER['SCRIPT_FILENAME']);
    if(basename($_SERVER['SCRIPT_NAME'])===$scriptName)
        $_scriptUrl=$_SERVER['SCRIPT_NAME'];
    else if(basename($_SERVER['PHP_SELF'])===$scriptName)
        $_scriptUrl=$_SERVER['PHP_SELF'];
    else if(isset($_SERVER['ORIG_SCRIPT_NAME']) && basename($_SERVER['ORIG_SCRIPT_NAME'])===$scriptName)
        $_scriptUrl=$_SERVER['ORIG_SCRIPT_NAME'];
    else if(($pos=strpos($_SERVER['PHP_SELF'],'/'.$scriptName))!==false)
        $_scriptUrl=substr($_SERVER['SCRIPT_NAME'],0,$pos).'/'.$scriptName;
    else if(isset($_SERVER['DOCUMENT_ROOT']) && strpos($_SERVER['SCRIPT_FILENAME'],$_SERVER['DOCUMENT_ROOT'])===0)
        $_scriptUrl=str_replace('\\','/',str_replace($_SERVER['DOCUMENT_ROOT'],'',$_SERVER['SCRIPT_FILENAME']));
    else
        throw new Exception('The App is unable to determine the entry script URL.'));
    return rtrim(dirname($_scriptUrl), '\\/');
}

I hope this helps someone else.

OTHER TIPS

What kind of syrup did you "pour" over your php.ini file? It may have gummed up the works! ;)

Have you looked in the output HTML via your browser (View > Page Source), and what you're getting is http://js/file.js? Is that src="http://js/file.js" in a tag? Adding http://domain.com/ should be the work of the browser, not the server. Some browsers display URLs with the domain already added, while others show exactly what you sent to the page. Are you sending any tags that maybe are missing the domain? I think it's only supposed to apply to relative URIs, but it's worth checking. Did you actually use js/file.js or /js/file.js? They're very different.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top