Welcome to collectivesolver - Programming & Software Q&A. A website you can trust. All programs tested and works. Contact: aviboots(AT)netvision.net.il
BlueHost Web Hosting

Bonsai Boy of New York Bonsai Trees and Accessories

Liquid Web iThemes WordPress Hosting

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

DreamHost Web Hosting


getResponse autofunnels

Liquid Web Cloud VPS Hosting

Disclosure We are a professional Information website that receives compensation from some of the links whose we show you with Information. It means that my content may contain affiliate links.

12,110 questions

16,498 answers

573 users

How to convert relative URL path to absolute URL in PHP

2 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
...