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

  • SEARCH
  • Home
  • 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 8353183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:10:59+00:00 2026-06-09T09:10:59+00:00

I am trying to get the siteurl without hardcoding and store it in a

  • 0

I am trying to get the siteurl without hardcoding and store it in a constant inside of a configuration file, so that I can use it within my scripts irrespective of the nesting/nested folders. This configuration file is stored in a folder within the main installation directory of the website.

Example:

Install website on http://www.mydomain.com

Configuration file located in http://www.mydomain.com/my_folder/configuration.php

Want to declare a constant in configuration.php file so that it always gets the installed path dynamically without having to hardcode it. So define("MY_SITE_URL", ???? )

What should be in the place of the ???? in the code above so that it works even if my main website is installed within nested folders?

Examples: All the pages will contain the same configuration.php at the beginning of the file as a require

Install website in Site 1:

Root directory of website: http://www.mydomain.com/demos/site1/

Location of configuration: http://www.mydomain.com/demos/site1/my_folder/configuration.php

Sub folder: http://www.mydomain.com/demos/site1/1/index.php

echoing MY_SITE_URL in index.php should give http://www.mydomain.com/demos/site1/

Sub folder: http://www.mydomain.com/demos/site1/1/2/index.php

Location of configuration: http://www.mydomain.com/demos/site1/my_folder/configuration.php

echoing MY_SITE_URL in index.php should give http://www.mydomain.com/demos/site1/

Install website in Site 2:

Root directory of website: http://www.mydomain.com/demos/site2/

Location of configuration: http://www.mydomain.com/demos/site2/my_folder/configuration.php

Sub folder: http://www.mydomain.com/demos/site2/1/index.php

echoing MY_SITE_URL in index.php should give http://www.mydomain.com/demos/site2/

Sub folder: http://www.mydomain.com/demos/site2/1/2/index.php

Location of configuration: http://www.mydomain.com/demos/site2/my_folder/configuration.php

echoing MY_SITE_URL in index.php should give http://www.mydomain.com/demos/site2/

Please note that the TLD can change. That means, installing the website on http://www.mydomain.tk, http://www.mydomai.org, http://www.mydomain.net, http://www.mydomain.co.ca etc. should not render the code useless.

WHAT I HAVE SO FAR:

I have tried to put together a script. I almost got there, but not 100%. Here it is:

function domain_url()
{
    $pageURL = 'http';
    if(isset($_SERVER["HTTPS"]))
    if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
    $pageURL .= "://";
    if ($_SERVER["SERVER_PORT"] != "80") 
    {
        $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
    } 
    else 
    {
        $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
    }

    //Remove any GET params from url
    $remmove_get = strtok($pageURL, '?');
    //Removes trailing slash only at the end of string. This slash is received only when accessing a page within sub folders.
    $pageURL = preg_replace('{/$}', '', $remmove_get);

    //Remove page name
    $pageURL = dirname($pageURL);

    return $pageURL;
}
define("MY_SITE_URL", domain_url().'/'); 

Issue with my current code:

Running the code on http://www.mydomain.com/demos/site1/ and http://www.mydomain.com/demos/site1/1/ gives MY_SITE_URL as http://www.mydomain.com/demos/site1/ and this is correct.

But when I run the code on http://www.mydomain.com/demos/site1/1/2/index.php, echoing MY_SITE_URL gives me http://www.mydomain.com/demos/site1/1/ when in fact I wanted: http://www.mydomain.com/demos/site1/

How to solve this?

  • 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-06-09T09:11:01+00:00Added an answer on June 9, 2026 at 9:11 am

    If you always want the 2nd directory then you can use

    $boom = explode("/", $_SERVER['REQUEST_URI']);
    $domain_url = $_SERVER["SERVER_NAME"].'/'.$boom[1].'/'.$boom[2];
    echo $domain_url;
    

    If the site is ever not in the 2nd directory you can use: (this code should be a bit more concise). Just replace site1 with the name of the site

    $site_search = "site1";
    $url = $_SERVER["SERVER_NAME"].$_SERVER['REQUEST_URI'];
    $domain_url = substr($url, 0, strpos($url, $site_search)+strlen($site_search));;
    echo $domain_url;   
    

    Update
    This will get the url for everything before /my_folder

    $domain_url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
    $domain_url = substr($url, 0, strpos($url, '/my_folder'));
    define("MY_SITE_URL", domain_url().'/'); 
    

    On a side-note, you should try the Code-Igniter framework. The framework makes this sort of thing extremely easy.

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

Sidebar

Related Questions

I am trying get all html links within a string and replace them using
I am trying get all html links within a string and replace them using
Trying to get this expression to work, can someone look at it and tell
Trying to get these list headings to line up correctly, but I can't figure
Im trying to use the sdk without a canvas application, so have followed steps
I am trying to put this function ((click_add)) in library so that i can
I'm trying to send a file to the user using xsendfile within the code
Using CodeIgniter I am trying to create a link that the user can click
So i am trying get 2 div-containers which both should contain centered text (Both
I'm trying get the visible portion of UIImage from an UIImageView . UIImageView takes

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.