PHP: Relative URL to Absolute URL

Recently, I was working on a project and I needed to convert the relative URLs to their absolute URLs. An absolute URL such as “http://www.example.com/image.jpg” but mostly written as only “image.jpg” (relative URL) on the web pages. Web browser creates a complete URL using this relative URL and base URL. The base URL, must be absolute and it is often the URL of the web page containing the relative URL.

Now, if you need to access a file whose relative URL and base URL are known, you must combine them to create an absolute URL.

The RFC URL specification defines an “absolutize” algorithm for combining an absolute base URL with a relative URL to create a new absolute URL. This algorithm was already implemented and I found the PHP script for the algorithm at nadeausoftware.com. I have created a project UrlToAbsolute at sourceforge, as I needed to use it in my several open source projects including FeedAPI ImageGrabber, Facebook-style Links and Feeds Image Grabber. As the original script was released under BSD License, it was pretty easy to fork it. Here is the link to the project UrlToAbsolute. You can also look at the step by step explanation of the script here.

Usage Instructions

Extract the script (url_to_absolute.php) into your web directory, include it into your current php file using:

require(path-to-file);

then, you can convert the relative url to absolute url by calling:

url_to_absolute( $baseUrl, $relativeUrl);

It return false on failure, otherwise returns the absolute url. If the $relativeUrl is a valid absolute url, it is returned without any modification.

Related resources:

ChangeLog

  • v1.6, March 12, 2010
    – added encode_url function to convert an absolute url to its percentage
    encoded equivalent, according to RFC 3986 (http://publicmind.in/blog/url-encoding/)
  • v1.5, March 11, 2010
    – fixed to allow spaces in the path of url
  • v1.4, October 2, 2009
    – Percentage encoding of the absolute url disabled.
  • v1.0, February 27, 2009
    – Initial release of the script on nadeausoftware.com

16 thoughts on “PHP: Relative URL to Absolute URL

  1. Pingback: Drupal: FeedAPI Imagegrabber | Public Mind

  2. Jay

    Hi there Nitin I’m testing this class as the script I’m puting together needs the ab/path to function, and wondering if you have any examples, thanks

    Reply
    1. nitin

      hi,

      as mentioned, it is very simple to use this function. just include it in your current file and call the function as mentioned above. You can look for the tutorial at nadeausoftware.com or the project i am currently using it in, feedapi imagegrabber.

      regards,

      Reply
  3. Tyler Akins

    This function doesn’t really work well for file:/// URLs. For instance, url_to_absolute(‘file:///path/file.html’, ‘image.gif’) returns false. Would you be able to change line 82 a bit so that if the scheme is ‘file’, it won’t require ‘host’?

    (Yeah, I can make the change locally, but I’d rather the change gets back into the main distribution.)

    Reply
  4. Tyler Akins

    Oh, I’m sorry that I didn’t mention this in my last post, but why don’t you use parse_url() instead of your split_url()? I’m a fan of using built-in functions when possible for speed and because a larger developer base will be continually reviewing its functionality.

    Reply
  5. Dinar Q.

    i have tried to modify this way : function join_url( $parts, $encode=TRUE) – does not help – also does not work.
    it returns empty string, as i remember.

    Reply
    1. Nitin Post author

      The complexity in the Dave’s solution has been added intentionally and well explained. If you read the tutorial, you would know why he avoided using the

      parse_url

      function and wrote his custom functions.

      Although I am not sure how many of his assumptions are still valid, it has been long when I looked at the code (it works so well!!) 🙂

      Reply
  6. NubbyNubkins

    I was experimenting with this script to see if my solution was slower and it definitely was. My solution was also far more complex. Thank you very much for offering this invaluable tool.
    I would also like to mention that this script sometimes has a problem dealing with empty values. While this may not often occur for many people, the way I wrote my link spider would result in empty array values when duplicates or non-page links were found. Because of the location within my script where I needed the absolute url conversion, empty values would appear directly prior to this function. The e-warning would occur on line 327: “return $parts”. and directly under the “if (!$decode)”. The simple solution I used was to include:

    if(($var != “”) || (!empty($var))) {url_to_absolute( $baseUrl, $relativeUrl);}

    I just thought you might want to know that there is a potential issue when it concerns empty values.

    Reply
  7. Dinar Qurbanov

    i have found an other bug:
    url_remove_dot_segments(‘/test/test/..’) gives /test but it should be /test/
    url_remove_dot_segments(‘/test/test/.’) gives /test/test but it should be /test/test/

    Reply
  8. Pingback: PHP: How to resolve a relative url - ExceptionsHub

Leave a Reply