Codeigniter - SEO Friendly URL Structure (Slug Implementation)

Codeigniter - SEO Friendly URL Structure (Slug Implementation)

October 19, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Codeigniter - SEO Friendly URL Structure (Slug Implementation)

Creating SEO-Friendly URL Structures Using Slugs in CodeIgniter

URL slugging is an essential practice for building search engine optimized websites. It not only makes URLs clean and readable but also improves the overall site structure from an SEO perspective. In this post, we’ll explore how to implement a slug system in your CodeIgniter application that allows you to access pages via friendly URLs like www.yoursite.com/category/slug.

Firstly, let’s understand what a ‘slug’ is: it’s essentially a permalink or URL-friendly version of a title, stripped of any special characters and spaces.

Building the Slug Table in CodeIgniter

For our example, assume we have a ‘posts’ table with the following schema:

  • id (INT)
  • title (VARCHAR)
  • content (TEXT)
  • slug (VARCHAR)

The slug field will store the SEO-friendly version of each post’s title.

Generating Slugs in CodeIgniter

CodeIgniter provides a built-in helper function called url_title() that converts text into URL slugs. Here’s how you can use it:

$title = "This is a Sample Blog Post";
$slug = url_title($title, 'dash', TRUE);

In this example:

  • $title contains the original title.
  • url_title() generates a slug using dashes as separators ('dash' parameter).
  • The third argument (boolean) ensures that all special characters are converted to their ASCII equivalents.

Saving Slugs to Database

When inserting or updating records, save the generated slug:

$data = array(
    'title' => $post_title,
    'content' => $post_content,
    'slug' => url_title($post_title, 'dash', TRUE)
);
$this->db->insert('posts', $data);

Fetching Data via Slug

To fetch a post by its slug, you might have code like this in your controller:

public function view_post()
{
    $slug = $this->uri->segment(3); // Get the third segment (slug)
    $query = $this->db->get_where('posts', array('slug' => $slug));
    $post = $query->row();

    if ($post) {
        // Display post content
    } else {
        show_404();
    }
}

Conclusion

Implementing slugs in your CodeIgniter application isn’t complex, but it significantly improves the URL structure and SEO of your site. Remember to:

  1. Create a slug field in your relevant database tables.
  2. Generate slugs using url_title() when saving records.
  3. Fetch data based on the slug rather than just an ID.

This approach keeps your URLs clean, memorable, and search-friendly. If you’d like to see this implemented in a real-world project, check out some of our case studies where we’ve applied similar techniques for clients across various industries.

Happy coding!

Catalog Database Performance PHP

Our network