David Doran Media

Search


[PHP] URL Resolver Open Source

While doing some work on the Page Check project I needed a way to make an absolute URL from a base URL and a relative one. So for instance if I knew the current page's URL was http://example.com/etc/example.html and the link was ../home.html then the final result would be http://example.com/home.html.

However for the life of me I couldn't find a PHP function that would do this for me so I wrote one that should resolve URLs according to RFC 1808.

Using abs_url

Simply call the function abs_url with your base URL and relative URL and it will return the result:
 
 echo abs_url(
 'http://www.example.com/directory1/directory2/page.php?list=all&'
 ,'./../directory3/upperpage.php?list=simple'
 );
 
will display:
http://www.example.com/directory1/directory3/upperpage.php?list=simple