URL Shortening with CodeIgniter

If you are looking for a fast way to URL shorten with CodeIgniter you are in luck.

Basically you set your routes (application->config->routes.php) $route[‘404_override’] to point to the controller and action which directs shorten URLs

e.g. in you routes file:

$route['404_override']='short_links/retrive';

Then short_links has a function retrive

<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Short_links extends CI_Controller {

    function retrive()
    {
        $this->load->model('shorten_urls');//load the model which looks after the URLs
        $url = $this->uri->segment(1);//get the URL segment e.g. abc
        redirect($this->shorten_urls->get_url($url)); //lookup the original URL and redirect
    }
}

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s