How to convert relative URL path to absolute URL in PHP

3 Answers

0 votes
function relative_path_to_absolute_url($relative_url, $base_url) {
    if (parse_url($relative_url, PHP_URL_SCHEME) != '') { 
        return $relative_url;
    }
    
    if ($relative_url[0] == '?' || $relative_url[0] == '#') { 
        return $base_url.$relative_url;
    }
     
    $relative_url = str_replace("../", "", $relative_url);
    
    extract(parse_url($base_url)); // parse $scheme, $host, $path
      
    if (empty($path)) {
        $path = "/";
    }
     
    $absolute_url = "$host$path/$relative_url";
    
    $arr = array('#(/\.?/)#', '#/(?!\.\.)[^/]+/\.\./#');
        
    for ($i = 1; $i > 0; $absolute_url = preg_replace($arr, '/', $absolute_url, -1, $i)) {}
    
    return $scheme.'://'.$absolute_url;
}
    
        
$relative_url = "/26332/how-to-get-the-last-word-from-a-string-in-javascript";
$base_url = "https://www.collectivesolver.com/";
$url = relative_path_to_absolute_url($relative_url, $base_url);
echo  "<br />" . $url . "<br />";
  
$relative_url = "/php";
$base_url = "https://www.collectivesolver.com/tag";
$url = relative_path_to_absolute_url($relative_url, $base_url);
echo  "<br />" . $url . "<br />";
  
$relative_url = "/php?q=ide";
$base_url = "https://www.collectivesolver.com";
$url = relative_path_to_absolute_url($relative_url, $base_url);
echo  "<br />" . $url . "<br />";
  
$relative_url = "/php?q=editor";
$base_url = "https://www.collectivesolver.com/public";
$url = relative_path_to_absolute_url($relative_url, $base_url);
echo  "<br />" . $url . "<br />";
  
$relative_url = "python";
$base_url = "https://www.collectivesolver.com/tag";
$url = relative_path_to_absolute_url($relative_url, $base_url);
echo  "<br />" . $url . "<br />";
 
$relative_url = "about.html";
$base_url = "https://www.collectivesolver.com/";
$url = relative_path_to_absolute_url($relative_url, $base_url);
echo  "<br />" . $url . "<br />";
 
$relative_url = "/experts/";
$base_url = "https://www.collectivesolver.com";
$url = relative_path_to_absolute_url($relative_url, $base_url);
echo  "<br />" . $url . "<br />";
 
$relative_url = "../";
$base_url = "https://www.collectivesolver.com";
$url = relative_path_to_absolute_url($relative_url, $base_url);
echo  "<br />" . $url . "<br />";

$relative_url = "../c++/";
$base_url = "https://www.collectivesolver.com";
$url = relative_path_to_absolute_url($relative_url, $base_url);
echo  "<br />" . $url . "<br />";

$relative_url = "../../../";
$base_url = "https://www.collectivesolver.com";
$url = relative_path_to_absolute_url($relative_url, $base_url);
echo  "<br />" . $url . "<br />";
 
$relative_url = "./questions.php";
$base_url = "https://www.collectivesolver.com";
$url = relative_path_to_absolute_url($relative_url, $base_url);
echo  "<br />" . $url . "<br />";
    
        
/*
run:
        
https://www.collectivesolver.com/26332/how-to-get-the-last-word-from-a-string-in-javascript

https://www.collectivesolver.com/tag/php

https://www.collectivesolver.com/php?q=ide

https://www.collectivesolver.com/public/php?q=editor

https://www.collectivesolver.com/tag/python

https://www.collectivesolver.com/about.html

https://www.collectivesolver.com/experts/

https://www.collectivesolver.com/

https://www.collectivesolver.com/c++/

https://www.collectivesolver.com/

https://www.collectivesolver.com/questions.php
        
*/

 



answered Sep 14, 2019 by avibootz
edited Sep 21, 2019 by avibootz
0 votes
function relative_path_to_absolute_url($relative_url, $base_url) {
    if (parse_url($relative_url, PHP_URL_SCHEME) != '') { 
        echo "1. relative_url = " . $relative_url . "<br />";
        return $relative_url;
    }
     
    if ($relative_url[0] == '?' || $relative_url[0] == '#') { 
        echo "2. relative_url[0] = " . $relative_url[0] . "<br />";
        return $base_url.$relative_url;
    }
     
    $relative_url = str_replace("../", "", $relative_url);
    echo "3. relative_url = " . $relative_url . "<br />";
     
    extract(parse_url($base_url)); // parse $scheme, $host, $path
    echo "4. scheme = " . $scheme . "<br />";
    echo "5. host = " . $host . "<br />";
    if (isset($path)) echo "6. path = " . $path . "<br />";
     
    if (empty($path)) {
        $path = "/";
        echo "7. path = " . $path . "<br />";
    }
      
    $absolute_url = "$host$path/$relative_url";
     
    $arr = array('#(/\.?/)#', '#/(?!\.\.)[^/]+/\.\./#');
         
    for ($i = 1; $i > 0; $absolute_url = preg_replace($arr, '/', $absolute_url, -1, $i)) {
        echo "8. absolute_url = " . $absolute_url . "<br />";
    }
     
    return $scheme.'://'.$absolute_url;
}
     
         
$relative_url = "14980/how-to-use-const-pointers-in-c";
$base_url = "https://www.collectivesolver.com/";
echo "relative_url = " . $relative_url . "<br />";
echo "base_url = " . $base_url . "<br />";
$url = relative_path_to_absolute_url($relative_url, $base_url);
echo  "<br />" . $url . "<br />-----<br /><br />";
   
$relative_url = "/php";
$base_url = "https://www.collectivesolver.com/tag";
echo "relative_url = " . $relative_url . "<br />";
echo "base_url = " . $base_url . "<br />";
$url = relative_path_to_absolute_url($relative_url, $base_url);
echo  "<br />" . $url . "<br />-----<br /><br />";
  
$relative_url = "/php?q=ide";
$base_url = "https://www.collectivesolver.com";
echo "relative_url = " . $relative_url . "<br />";
echo "base_url = " . $base_url . "<br />";
$url = relative_path_to_absolute_url($relative_url, $base_url);
echo  "<br />" . $url . "<br />-----<br /><br />";
   
$relative_url = "/php?q=editor";
$base_url = "https://www.collectivesolver.com/public";
echo "relative_url = " . $relative_url . "<br />";
echo "base_url = " . $base_url . "<br />";
$url = relative_path_to_absolute_url($relative_url, $base_url);
echo  "<br />" . $url . "<br />-----<br /><br />";
  
$relative_url = "python";
$base_url = "https://www.collectivesolver.com/tag";
echo "relative_url = " . $relative_url . "<br />";
echo "base_url = " . $base_url . "<br />";
$url = relative_path_to_absolute_url($relative_url, $base_url);
echo  "<br />" . $url . "<br />-----<br /><br />";
 
$relative_url = "about.html";
$base_url = "https://www.collectivesolver.com/";
echo "relative_url = " . $relative_url . "<br />";
echo "base_url = " . $base_url . "<br />";
$url = relative_path_to_absolute_url($relative_url, $base_url);
echo  "<br />" . $url . "<br />-----<br /><br />";
  
$relative_url = "/experts/";
$base_url = "https://www.collectivesolver.com";
echo "relative_url = " . $relative_url . "<br />";
echo "base_url = " . $base_url . "<br />";
$url = relative_path_to_absolute_url($relative_url, $base_url);
echo  "<br />" . $url . "<br />-----<br /><br />";
  
$relative_url = "../";
$base_url = "https://www.collectivesolver.com";
echo "relative_url = " . $relative_url . "<br />";
echo "base_url = " . $base_url . "<br />";
$url = relative_path_to_absolute_url($relative_url, $base_url);
echo  "<br />" . $url . "<br />-----<br /><br />";
   
$relative_url = "../c++/";
$base_url = "https://www.collectivesolver.com";
echo "relative_url = " . $relative_url . "<br />";
echo "base_url = " . $base_url . "<br />";
$url = relative_path_to_absolute_url($relative_url, $base_url);
echo  "<br />" . $url . "<br />-----<br /><br />";
 
$relative_url = "../../../";
$base_url = "https://www.collectivesolver.com";
echo "relative_url = " . $relative_url . "<br />";
echo "base_url = " . $base_url . "<br />";
$url = relative_path_to_absolute_url($relative_url, $base_url);
echo  "<br />" . $url . "<br />-----<br /><br />";
  
$relative_url = "./questions.php";
$base_url = "https://www.collectivesolver.com";
echo "relative_url = " . $relative_url . "<br />";
echo "base_url = " . $base_url . "<br />";
$url = relative_path_to_absolute_url($relative_url, $base_url);
echo  "<br />" . $url . "<br />-----<br /><br />";

   
     
         
/*
run:
         
relative_url = 14980/how-to-use-const-pointers-in-c
base_url = https://www.collectivesolver.com/
3. relative_url = 14980/how-to-use-const-pointers-in-c
4. scheme = https
5. host = www.collectivesolver.com
6. path = /
8. absolute_url = www.collectivesolver.com//14980/how-to-use-const-pointers-in-c
8. absolute_url = www.collectivesolver.com/14980/how-to-use-const-pointers-in-c

https://www.collectivesolver.com/14980/how-to-use-const-pointers-in-c
-----

relative_url = /php
base_url = https://www.collectivesolver.com/tag
3. relative_url = /php
4. scheme = https
5. host = www.collectivesolver.com
6. path = /tag
8. absolute_url = www.collectivesolver.com/tag//php
8. absolute_url = www.collectivesolver.com/tag/php

https://www.collectivesolver.com/tag/php
-----

relative_url = /php?q=ide
base_url = https://www.collectivesolver.com
3. relative_url = /php?q=ide
4. scheme = https
5. host = www.collectivesolver.com
7. path = /
8. absolute_url = www.collectivesolver.com///php?q=ide
8. absolute_url = www.collectivesolver.com//php?q=ide
8. absolute_url = www.collectivesolver.com/php?q=ide

https://www.collectivesolver.com/php?q=ide
-----

relative_url = /php?q=editor
base_url = https://www.collectivesolver.com/public
3. relative_url = /php?q=editor
4. scheme = https
5. host = www.collectivesolver.com
6. path = /public
8. absolute_url = www.collectivesolver.com/public//php?q=editor
8. absolute_url = www.collectivesolver.com/public/php?q=editor

https://www.collectivesolver.com/public/php?q=editor
-----

relative_url = python
base_url = https://www.collectivesolver.com/tag
3. relative_url = python
4. scheme = https
5. host = www.collectivesolver.com
6. path = /tag
8. absolute_url = www.collectivesolver.com/tag/python

https://www.collectivesolver.com/tag/python
-----

relative_url = about.html
base_url = https://www.collectivesolver.com/
3. relative_url = about.html
4. scheme = https
5. host = www.collectivesolver.com
6. path = /
8. absolute_url = www.collectivesolver.com//about.html
8. absolute_url = www.collectivesolver.com/about.html

https://www.collectivesolver.com/about.html
-----

relative_url = /experts/
base_url = https://www.collectivesolver.com
3. relative_url = /experts/
4. scheme = https
5. host = www.collectivesolver.com
7. path = /
8. absolute_url = www.collectivesolver.com///experts/
8. absolute_url = www.collectivesolver.com//experts/
8. absolute_url = www.collectivesolver.com/experts/

https://www.collectivesolver.com/experts/
-----

relative_url = ../
base_url = https://www.collectivesolver.com
3. relative_url =
4. scheme = https
5. host = www.collectivesolver.com
7. path = /
8. absolute_url = www.collectivesolver.com//
8. absolute_url = www.collectivesolver.com/

https://www.collectivesolver.com/
-----

relative_url = ../c++/
base_url = https://www.collectivesolver.com
3. relative_url = c++/
4. scheme = https
5. host = www.collectivesolver.com
7. path = /
8. absolute_url = www.collectivesolver.com//c++/
8. absolute_url = www.collectivesolver.com/c++/

https://www.collectivesolver.com/c++/
-----

relative_url = ../../../
base_url = https://www.collectivesolver.com
3. relative_url =
4. scheme = https
5. host = www.collectivesolver.com
7. path = /
8. absolute_url = www.collectivesolver.com//
8. absolute_url = www.collectivesolver.com/

https://www.collectivesolver.com/
-----

relative_url = ./questions.php
base_url = https://www.collectivesolver.com
3. relative_url = ./questions.php
4. scheme = https
5. host = www.collectivesolver.com
7. path = /
8. absolute_url = www.collectivesolver.com//./questions.php
8. absolute_url = www.collectivesolver.com/./questions.php
8. absolute_url = www.collectivesolver.com/questions.php

https://www.collectivesolver.com/questions.php
-----
       
*/

 



answered Sep 19, 2019 by avibootz
edited Sep 21, 2019 by avibootz
0 votes
function createAbsoluteURL($src, $url) {
    $scheme = parse_url($url)["scheme"];
    $host   = parse_url($url)["host"];

    if (substr($src, 0, 2) == "//") {
        $src = $scheme . ":" . $src;
    } else if (substr($src, 0, 1) == "/") {
        $src = $scheme . "://" . $host . $src;
    } else if (substr($src, 0, 2) == "./") {
        $dirname = dirname(parse_url($url)["path"]);
        $dirname = ($dirname == "\\" || $dirname == "/") ? "" : $dirname;
        $src     = $scheme . "://" . $host . $dirname . substr($src, 1);
    } else if (substr($src, 0, 3) == "../") {
        $src = $scheme . "://" . $host . substr($src, 2);
    } else if (substr($src, 0, 5) != "https" && substr($src, 0, 4) != "http") {
        $src = $scheme . "://" . $host . "/" . $src;
    }

    return $src;
}

$url = "https://www.collectivesolver.com/";
echo createAbsoluteURL("//www.collectivesolver.com", $url) . "<br>";
echo createAbsoluteURL("/about.html", $url) . "<br>";
echo createAbsoluteURL("./test.php", $url) . "<br>";
echo createAbsoluteURL("../abc.php", $url) . "<br>";
echo createAbsoluteURL("xyz.php", $url) . "<br><br>";

$url = "http://localhost/allonpage/";
echo createAbsoluteURL("//www.collectivesolver.com", $url) . "<br>";
echo createAbsoluteURL("/about.html", $url) . "<br>";
echo createAbsoluteURL("./test.php", $url) . "<br>";
echo createAbsoluteURL("../abc.php", $url) . "<br>";
echo createAbsoluteURL("xyz.php", $url) . "<br>";




/*
run:

https://www.collectivesolver.com
https://www.collectivesolver.com/about.html
https://www.collectivesolver.com/test.php
https://www.collectivesolver.com/abc.php
https://www.collectivesolver.com/xyz.php

http://www.collectivesolver.com
http://localhost/about.html
http://localhost/test.php
http://localhost/abc.php
http://localhost/xyz.php

*/

 



answered Jul 12, 2020 by avibootz
edited Jul 12, 2020 by avibootz
...