Categories: Learn CodeIgniter Tutorials, Tips And GuidesLearn PHP Tutorials, Tips And Guides

Integrate Cloudways CDN with CodeIgniter Projects

Reading Time: 3 minutes

Content Distribution Network or CDN has become an important aspect of website design. Given the global nature of the audience, a CDN is invaluable in ensuring fast page load speed for all visitors, regardless of the location of the visitor.

A CDN stores a copy of the static elements of the website on a series of servers called nodes. When a user requests a web page, the static elements are served from the nearest node of the CDN. This reduces the page load time significantly.

The good news is that you could easily integrate CDN within your CodeIgniter projects built on any PHP MySQL Hosting. This tutorial will guide you through the process of installing CloudwaysCDN on a Codeigniter project hosted on Cloudways.

The first step is the launch of a server and a PHP application on Cloudways. For more details on this, please check out the following GIF:

Login to your server and go to the Applications tab. Click the PHP application. Once the new screen opens, the tab for configuring CDN at the end of the options in the left-side panel.

Once you click the link, you will be redirected to the CDN screen where you could get the newly generated URL which will be used in the code application.

Now you need to extend the existing cdn_base_url function by overwriting it.

For this, start by creating the file: application/helpers/cdn_helper.php

function cdn_base_url($uri)


   $currentInstance =& get_instance();

   $keybasedcdnUrl = $currentInstance->config->item('cdn_based_key_url');

   $extensions = array('css', 'js', 'jpg', 'jpeg', 'png', 'gif','pdf');

   $pathParts = pathinfo($uri);

   if (!empty($keybasedcdnUrl) && in_array($pathParts['extension'],$extensions)) {

       return $keybasedcdnUrl . $uri;


   return $currentInstance->config->cdn_base_url($uri);


This code will overwrite the cdn_base_url function to support and load the website assets from loading your asset from the key based CDN.

The next step is setting up the CDN URL. For this, you need to modify this file: applicaiton/config/config.php. Add the folloing line into the file . Remember to replace the cdn based url with the proper URL:

$config['cdn_based_key_url']   = 'cdn based url';


Here is the output in action:


In this tutorial, I discussed how you could integrate CDN with your CodeIgniter project. If you need help with the process, do leave a comment and I will get back to you.

Saquib Rizwan :Saquib is a PHP Community Expert at Cloudways - A Managed PHP Hosting Cloud Platform. He is well versed in PHP and regularly contributes to open source projects. For fun, he enjoys gaming, movies and hanging out with friends. You can email him at saquib.rizwan@cloudways.com