php.htaccessrelative-pathbase-tag

Problem Getting Root URL in PHP


I'm having a problem getting the root URL with this PHP function I composed. I can get it, but I have to make exceptions per server, but I desire one consistent function instead. Can you show me how to fix it?

I stick it in the root folder of a given website, which on my Ubuntu workstation would be under /var/www/website, but on my CentOS server it might be under /home/me/website. It works on my workstation, but on the server I have to make a different version for some reason. Also, I have yet another version I had to make for yet another CentOS server that didn't seem to like the function as well. At the tail end of this question, I also list some background information on why I'm doing this in the first place.

Workstation Version

public function getRootURL() {
  $sTemp = str_replace($_SERVER['DOCUMENT_ROOT'],'',__DIR__);
  $sTemp = str_replace($sTemp, '', $_SERVER['REQUEST_URI']);
  $sPageURL = (@$_SERVER['HTTPS'] == 'on') ? 'https://' . $_SERVER['SERVER_NAME'] : 'http://' . $_SERVER['SERVER_NAME'];
  $sPageURL .= ($_SERVER['SERVER_PORT'] != 80) ? ':' . $_SERVER['SERVER_PORT'] . $_SERVER['REQUEST_URI'] : $_SERVER['REQUEST_URI'];
  $sRootURL = str_replace($sTemp,'',$sPageURL);
  $sRootURL = rtrim($sRootURL, '/') . '/';
  $sRootURL = (strpos(' ' . $sRootURL, '://') === FALSE) ? $sPageURL : $sRootURL;
  return $sRootURL;
}

Server Version

public function getRootURL() {
  $sTemp = str_replace($_SERVER['DOCUMENT_ROOT'],'',__DIR__);
  $sTemp = str_replace($sTemp, '', $_SERVER['REQUEST_URI']);
  $sPageURL = (@$_SERVER['HTTPS'] == 'on') ? 'https://' . $_SERVER['SERVER_NAME'] : 'http://' . $_SERVER['SERVER_NAME'];
  $sPageURL .= ($_SERVER['SERVER_PORT'] != 80) ? ':' . $_SERVER['SERVER_PORT'] . $_SERVER['REQUEST_URI'] : $_SERVER['REQUEST_URI'];
  $sRootURL = str_replace($sTemp,'',$sPageURL);
  $sRootURL = rtrim($sRootURL, '/') . '/';
  $sRootURL = (strpos(' ' . $sRootURL, '://') === FALSE) ? $sPageURL : $sRootURL;
  if (strpos(' ' . $sRootURL, 'mycentosserver.com')>0) {
    $sRootURL .= 'website/';
  }
  return $sRootURL;
}

On the server version, notice the "mycentosserver.com" if/then statement -- that's the difference.

Alternative Server Version

function getRootURL() {
  $sTemp = str_replace($_SERVER['DOCUMENT_ROOT'],'',__DIR__);
  $sTemp = str_replace($sTemp, '', $_SERVER['REQUEST_URI']);
  $sPageURL = (@$_SERVER['HTTPS'] == 'on') ? 'https://' . $_SERVER['SERVER_NAME'] : 'http://' . $_SERVER['SERVER_NAME'];
  $sPageURL .= ($_SERVER['SERVER_PORT'] != 80) ? ':' . $_SERVER['SERVER_PORT'] . $_SERVER['REQUEST_URI'] : $_SERVER['REQUEST_URI'];
  $sRootURL = str_replace($sTemp,'',$sPageURL);
  $sRootURL = rtrim($sRootURL, '/') . '/';
  $sRootURL = (strpos($sRootURL, '://') === FALSE) ? $sPageURL : $sRootURL;
  $sRootURL .= 'website2/'; // heck, I give up -- strange Apache problem on this server
  return $sRootURL;
}

Background

Normally I would use an MVC framework, but I have a client who wants to edit things when I'm gone, and who doesn't like MVC and doesn't understand it. He has simplistic PHP skills. So, I'm using an .htaccess that looks like this for pretty URLs, and ones where slashes can come on the end of the URL, alternatively:

Options -Indexes +FollowSymlinks -MultiViews
RewriteEngine on

#No slash on the end of the url and not a real file/folder? then show x.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^[^/]*$ "$0.php" [nc]

#slashes on the end still and not a real file/folder, then remove them
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]

#if still here, and not a real file/folder, then show x.php in the alternative way
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ "$1.php" [nc]

And am creating subfolders like _css, _js, _classes, etc. -- trying to keep things fairly simple. Each page has like PHP at the top and then params at the bottom in the HTML. This is in an attempt at trying to eliminate spaghetti code. The client seems to like this, although I would prefer MVC, and especially for this root URL reason.

Note however with the .htaccess technique, there are cases where my HTML may not have proper relative pathing, such as someone adding a slash on the end of a URL, and therefore I must use a tag in the HTML to clear things up with relative pathing. This is why the getRootURL() function must work properly.


Solution

  • How about this?

    function getRootURL() {
      $protocol   = empty($_SERVER['HTTPS'])? 'http' : 'https';
      $servername = $_SERVER['SERVER_NAME'];
      $serverport = $_SERVER['SERVER_PORT']=='80'? '' : ':' . $_SERVER['SERVER_PORT'];
      $path       = str_replace('\\', '/', substr(dirname(__FILE__), strlen($_SERVER['DOCUMENT_ROOT'])));
    
      return $protocol . '://' . $servername . $serverport . $path;
    }
    

    __FILE__ should work as long as the file is really in the root of the project (otherwise add a few more dirname()s.


    Btw, if you are worried about "someone adding a slash on the end of a URL", why don't you just add /? to your RewriteRule?