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:
then, you can convert the relative url to absolute url by calling:
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:
- Project page: http://sourceforge.net/projects/absoluteurl
- Original article: http://nadeausoftware.com/articles/2008/05/php_tip_how_convert_relative_url_absolute_url
- 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
Pingback: Drupal: FeedAPI Imagegrabber | Public Mind
That’s exactly what I need. Thanks,
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
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,
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.)
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.
split_url() over parse_url(), explained here: http://bit.ly/diaXxL
Cheers,
address like http://kk.wiktionary.org/wiki/Басты_бет does not work, example of such link, in this page: http://tor.kz/index.php/%D0%91%D0%B0%D1%81%D1%82%D1%8B_%D0%B1%D0%B5%D1%82 .
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.
This one is way more simple, and even works for parent directories (..) and recognizes base direcories (.):
http://web-o-blog.blogspot.com/2011/04/function-absoluteurlbaseurl-relativeurl.html
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
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!!) 🙂
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.
My apologies. I forgot to clarify that the solution I used is not in the script itself but when the function is called.
Nice article I have used this PHP Script in my website and I have better performance in SEO also
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/
Pingback: PHP: How to resolve a relative url - ExceptionsHub