I am trying to get siteurl without hardcoding and store it in a constant inside the configuration file so that I can use it in my scripts regardless of subfolders / subfolders. This configuration file is stored in a folder in the main installation directory of the website.
Example:
Install the website at www.mydomain.com
Configuration file located at www.mydomain.com/my_folder/configuration.php
You want to declare a constant in the configuration.php file so that it always dynamically sets the path without requiring hard coding. So define("MY_SITE_URL", ???? )
What should be in place ???? in the code above to make it work, even if my main site is installed inside subfolders?
Examples. All pages will contain the same .php configuration file at the beginning of the file that is required
Set site on site 1:
The root directory of the website: www.mydomain.com/demos/site1/
Configuration Location: www.mydomain.com/demos/site1/my_folder/configuration.php
Subfolder: www.mydomain.com/demos/site1/1/index.php
Repeating MY_SITE_URL in index.php, you should specify www.mydomain.com/demos/site1/
Swap Folder: www.mydomain.com/demos/site1/1/2/index.php
Configuration Location: www.mydomain.com/demos/site1/my_folder/configuration.php
Repeating MY_SITE_URL in index.php, you should specify www.mydomain.com/demos/site1/
Set site on site 2:
Root directory of the website: www.mydomain.com/demos/site2/
Configuration Location: www.mydomain.com/demos/site2/my_folder/configuration.php
Subfolder: www.mydomain.com/demos/site2/1/index.php
the echo MY_SITE_URL in index.php should point to www.mydomain.com/demos/site2/
Subfolder: www.mydomain.com/demos/site2/1/2/index.php
Configuration Location: www.mydomain.com/demos/site2/my_folder/configuration.php
the echo MY_SITE_URL in index.php should point to www.mydomain.com/demos/site2/
Please note that TLDs are subject to change. This means setting up a website at www.mydomain.tk, www.mydomai.org, www.mydomain.net, www.mydomain.co.ca, etc. Should not make the code useless.
WHAT I AM FURTHER:
I tried to build a script. I almost got there, but not 100%. Here he 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().'/');
The problem with my current code is:
Running the code on www.mydomain.com/demos/site1/ and www.mydomain.com/demos/site1/1/ gives MY_SITE_URL as www.mydomain.com/demos/site1/, and rightly so.
But when I run the code at www.mydomain.com/demos/site1/1/2/index.php, the echo MY_SITE_URL gives me www.mydomain.com/demos/site1/1/ when I really wanted: www.mydomain.com / demos / site1 /
How to solve this?