Convert relative path into absolute url

I was developing a tool which extracts all links from webpage but I was getting relative path (e.g. /dir/page.html) instead of absolute url (e.g. http://www.example.com/dir/page.html). Most of websites use relative path to link internal page and browser can convert in into absolute url but php don’t have any inbuilt function to do this. So after searching internet I found one which can convert relative path into absolute url. This is one of the important function for php programmer but missing in php language

php function to convert relative path into absolute url

 function rel2abs($rel, $base)
  {
  if(strpos($rel,"//")===0)
  {
  return "http:".$rel;
  }
  /* return if  already absolute URL */
  if  (parse_url($rel, PHP_URL_SCHEME) != '') return $rel;
  /* queries and  anchors */
  if ($rel[0]=='#'  || $rel[0]=='?') return $base.$rel;
  /* parse base URL  and convert to local variables:
  $scheme, $host,  $path */
  extract(parse_url($base));
  /* remove  non-directory element from path */
  $path = preg_replace('#/[^/]*$#',  '', $path);
  /* destroy path if  relative url points to root */
  if ($rel[0] ==  '/') $path = '';
  /* dirty absolute  URL */
  $abs =  "$host$path/$rel";
  /* replace '//' or  '/./' or '/foo/../' with '/' */
  $re =  array('#(/.?/)#', '#/(?!..)[^/]+/../#');
  for($n=1; $n>0;  $abs=preg_replace($re, '/', $abs, -1, $n)) {}
  /* absolute URL is  ready! */
  return  $scheme.'://'.$abs;
  }

Uses & examples

  echo rel2abs("/dir/page.html"," http://www.example.com/");
  // Output: http://www.example.com/dir/page.html
  
  echo rel2abs("/dir/page.html"," http://www.example.com/dir1/page2.html");
  // Output: http://www.example.com/dir/page.html
  
  echo rel2abs("dir/page.html"," http://www.example.com/dir1/page2.html");
  // Output: http://www.example.com/dir1/dir/page.html
  
  echo rel2abs("../dir/page.html"," http://www.example.com/dir1/dir3/page.html");
  // Output: http://www.example.com/dir1/dir/page.html

Liked It? Get Free updates in your Email

Delivered by feedburner

2 thoughts on “Convert relative path into absolute url

  1. Benjamin Intal
    #

    Removal of “../” isn’t working. Here’s the revised code and reformatted to be somewhat readable:

    function rel2abs( $rel, $base ) {

    /* parse base URL and convert to local variables: $scheme, $host, $path */
    extract( parse_url( $base ) );

    if ( strpos( $rel,"//" ) === 0 ) {
    return $scheme . ':' . $rel;
    }

    /* return if already absolute URL */
    if ( parse_url( $rel, PHP_URL_SCHEME ) != '' ) {
    return $rel;
    }

    /* queries and anchors */
    if ( $rel[0] == '#' || $rel[0] == '?' ) {
    return $base . $rel;
    }

    /* remove non-directory element from path */
    $path = preg_replace( '#/[^/]*$#', '', $path );

    /* destroy path if relative url points to root */
    if ( $rel[0] == '/' ) {
    $path = '';
    }

    /* dirty absolute URL */
    $abs = $host$path . "/" . $rel;

    /* replace '//' or '/./' or '/foo/../' with '/' */
    $abs = preg_replace( "/(/.?/)/", "/", $abs );
    $abs = preg_replace( "//(?!..)[^/]+/..//", "/", $abs );

    /* absolute URL is ready! */
    return $scheme . '://' . $abs;
    }

    Reply
  2. Pingback: Converting Relative URLs to Absolute URLs in PHP - Gambit

Leave a Reply to Benjamin Intal Cancel Reply

Your email address will not be published. Required fields are marked *