←    Back

Convert Relative Path to Absolute URL with PHP

I was developing a Web Crawler with PHP. But whenever I was crawling a url to get the links from that url, I found that most of the websites use relative path to link to their internal pages. The browser is smart enough to convert those relative paths to absolute url and request the correct page whenever those relative paths are clicked. But, as it seems, PHP does not have any inbuilt support to convert those relative paths to absolute urls.

Therefore, I decided to define a method to do the job. Here I am sharing my method of converting relative path to absolute url.

The Base

The first thing to mention is the base url. The relative paths are, actually, relative to the base url. If the page contains any <base> tag with its href attribute set, the browser will treat that url as base url. Otherwise, the browser will treat the url of the current page as the base url.

Now first define a base url:

So we are assuming that we are currently on this page and this url is our base url as defined with $baseUrl.

The Method

As the base url is set, its time to start defining the method:

Here we will pass the relative path as the $relativeUrl and the base url as the $baseUrl. I decided to pass each and every path I encounter while crawling. Therefore the path may be an absolute one. So we have to keep that in mind.

Now here is the body of the method:

Now in the method you have found two another method uptoLastDir() and onlySitePath(). Here is those methods:


The Author

Other Articles

  • How to use Zaq: Codeigniter Template Parser Engine

    Zaq is a PHP based template parser engine developed to work with Codeigniter. This library has been developed for developers to integrate php codes in views easily. Using this library will also allow the view file to be more readable. View files in Codeigniter (or in any other framework following MVC) always contains both html and php codes which make them a bit harder to read. This problem can be eradicated by using a parser engine which makes the view files a lot more easier to work with.

  • Category Pagination in Jekyll

    This site is built on Jekyll – a blog aware static site generator. When I was building this, I wanted to separate my blog from my portfolio. Of course I would be using custom post type for my portfolio if I was developing on Wordpress. Here I thought to stay straight and simple and I just created two site categories: portfolio and blog and I am using custom permalink which makes it difficult to use the default pagination setup. And I also want to paginate only the blog category.

  • 15 Best Bootstrap Alternatives for Web Developers

    Choosing the right framework that is the perfect fit for your projects could be a little bit overwhelming – there are a lot to choose from. Perhaps, you want to go with popular choices like Bootstrap or Foundation, but if your website is going to be a fairly simple one, you won’t be needing most of the building blocks and materials included in the default package. The good news is that there are a handful of alternatives that are much leaner than Bootstrap or Foundation. Most of these frameworks ship with just the right amount of styles and components to help you get started, while allowing you to be able to extend them in the direction you want for your project.

  • 8 Icon Fonts to speed up Website Design Process

    Icon Fonts are nothing but fonts. But, instead of containing letters or numbers, they contain symbols and shapes. You can style them with CSS in the same way you style regular text. They’re ideal for small, frequently used shapes such as email, envelopes, telephones, widget controls and social media logos. Here is a list of most popular and easy-to-use icon font sets.

  • Download ZIP File Dynamically with PHP

    Here we will see how we can make a webpage act as an initializer to download a zip file. We will just provide the location of the file and PHP will download it to the user. In the back-end, the HTTP headers are responsible for the download. We will set the headers with PHP.

  • How to Add Scalable Vector Graphics (SVG) to Web Page

    Scalable Vector Graphics (SVG) is an XML-based vector image format for two-dimensional graphics with support for interactivity and animation. If you want to embed your hand-crafted SVG in your web page, you can use any of the methods discussed here.