Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4019078
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:03:41+00:00 2026-05-20T10:03:41+00:00

I’m having a problem getting the root URL with this PHP function I composed.

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T10:03:42+00:00Added an answer on May 20, 2026 at 10:03 am

    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?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is what i have right now Drawing an RSS feed into the php,
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have some data like this: 1 2 3 4 5 9 2 6
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.