Canonical for dynamic pages in CodeIgniter

What Is a Canonical URL?

A canonical URL is the preferred URL that you want search engines to associate with a page when the same or very similar content can be accessed through multiple URLs.

A canonical tag is placed inside the <head> section of a webpage:

<link rel="canonical" href="https://www.example.com/example-page">
Important: A canonical tag is a signal to search engines. It does not automatically redirect visitors from one URL to another.

Why Are Canonical URLs Important for SEO?

Dynamic websites can sometimes generate multiple URLs that show the same or very similar content.

For example:

https://www.example.com/product/mobile-phone


https://www.example.com/product/mobile-phone?sort=price

https://www.example.com/product/mobile-phone?utm_source=facebook

If these URLs display substantially the same page, a canonical URL can help indicate which URL is preferred.

  • Consolidates canonicalization signals.
  • Helps search engines understand URL preferences.
  • Can reduce ambiguity caused by URL variations.
  • Helps search engines focus on the preferred URL.
  • Is particularly useful on large dynamic websites.

Canonical URL vs 301 Redirect

Canonical301 Redirect
Tells search engines which URL is preferred. Redirects users and crawlers to another URL.
The alternate URL can remain accessible. The old URL normally redirects to the new URL.
Useful when multiple URLs need to remain accessible. Useful when an old URL has permanently moved.
Example: If two URLs need to remain accessible because of different parameters, canonicalization may be appropriate. If an old page has permanently moved to a new URL, a 301 redirect is usually more appropriate.

When Should You Use a Canonical Tag?

Canonical URLs are useful in situations such as:

  1. Duplicate or substantially similar pages.
  2. Product pages with tracking parameters.
  3. Sorting and filtering URLs.
  4. Dynamic URLs containing query parameters.
  5. Multiple URL variations for the same content.
  6. Content accessible through different URL structures.

For many normal pages, a self-referencing canonical is also a useful implementation.


Dynamic URLs in CodeIgniter

CodeIgniter websites commonly generate pages dynamically using controller methods, database records and URL parameters.

For example:

https://www.example.com/blog/what-is-seo


https://www.example.com/blog/what-is-seo?utm_source=google

https://www.example.com/blog/what-is-seo?utm_source=facebook

The content may remain the same while tracking parameters change. In such cases, you may want the clean URL to be the canonical URL.


Step 1 — Create a Canonical URL in CodeIgniter

The simplest approach is to generate the canonical URL dynamically from the current page URL.

Add the canonical tag inside your common header file:

<link rel="canonical" href="<?= $canonical_url; ?>">

Your controller can prepare the canonical URL and send it to the view.


Step 2 — Create a Canonical URL in the Controller

Suppose your CodeIgniter controller contains a dynamic blog page. You can create the canonical URL from the blog slug.

<?php


public function detail($slug)
{
$data['blog'] = $this->blog->get_blog_by_slug($slug);

$data['canonical_url'] = base_url('blog/' . $slug);

$this->load->view('blog-detail', $data);


}
Result: If the slug is what-is-seo, the canonical URL becomes:
https://www.example.com/blog/what-is-seo

Step 3 — Add Canonical Tag to the View

Now add the canonical tag inside the <head> section of your page.

<head>

<title><?= $blog->title; ?></title>

<link rel="canonical"
      href="<?= $canonical_url; ?>">



Step 4 — Create a Reusable Canonical Function

If your website has many dynamic pages, creating the canonical URL separately in every controller can become repetitive.

You can create a reusable helper function.

File:

application/helpers/seo_helper.php

Code:

<?php


if (!function_exists('canonical_url'))
{
function canonical_url($url = '')
{
return rtrim(base_url($url), '/');
}
}

Load the helper from autoload.php:

$autoload['helper'] = array('url', 'seo');

Then use:

<link rel="canonical"
  href="<?= canonical_url('blog/' . $blog->url); ?>">

Handling Query Parameters

Dynamic websites frequently use query parameters such as:

?utm_source=google


?utm_medium=social
?sort=price
?ref=facebook

If these parameters do not change the main content of the page, you may want your canonical URL to point to the clean URL.

For example:

Current URL:


https://www.example.com/course?utm_source=facebook

Canonical:
https://www.example.com/course
Important: Do not automatically remove every query parameter. Some parameters genuinely change the content of a page. Canonicalization should reflect your site's intended URL structure.

Self-Referencing Canonical

A page can also contain a canonical tag pointing to itself. This is called a self-referencing canonical.

Example:

<link rel="canonical"
  href="https://www.example.com/about-us">

On the About Us page, the canonical URL points to the About Us URL.

Good practice: Keep your canonical URLs consistent with the URLs you actually want indexed and shown in search results.

HTTPS Canonical URLs

If your website uses HTTPS, make sure the canonical URL uses HTTPS as well.

Correct:

<link rel="canonical"
  href="https://www.example.com/blog/example">

Avoid:

<link rel="canonical"
  href="http://www.example.com/blog/example">

Handling index.php in CodeIgniter

Older CodeIgniter installations may generate URLs containing index.php.

https://www.example.com/index.php/blog/example

If your website's preferred public URL is:

https://www.example.com/blog/example

your canonical should use the preferred clean URL.

Tip: Canonical tags should not be used as a substitute for properly configuring your site's URL and redirect structure.

Complete CodeIgniter Dynamic Blog Example

Controller — Blogs.php

<?php


class Blogs extends CI_Controller
{
public function detail($slug)
{
$data['blog'] = $this->blog->get_blog_by_slug($slug);

    if (!$data['blog'])
    {
        show_404();
    }

    $data['canonical_url'] =
        base_url('blog/' . $data['blog']->url);

    $this->load->view('blog/detail', $data);
}


}

View — detail.php

<!DOCTYPE html>

<title><?= $blog->title; ?></title>

<meta name="description"
      content="<?= htmlspecialchars($blog->description, ENT_QUOTES, 'UTF-8'); ?>">

<link rel="canonical"
      href="<?= $canonical_url; ?>">

 

Common Canonical Tag Mistakes

  1. Using an incorrect or broken canonical URL.
  2. Using HTTP when the website uses HTTPS.
  3. Pointing every page to the homepage.
  4. Creating canonical URLs that redirect to another URL.
  5. Using different canonical URLs for equivalent page versions without a clear reason.
  6. Using canonical tags instead of 301 redirects when a page has permanently moved.
  7. Generating canonical URLs containing unnecessary tracking parameters.
  8. Creating canonical URLs for pages that do not represent the preferred content.

Canonical URL SEO Checklist


How to Check a Canonical URL

Method 1 — View Page Source

Open your webpage and use View Page Source. Search for:

rel="canonical"

You should find something similar to:

<link rel="canonical"
  href="https://www.example.com/blog/example">

Method 2 — Browser Developer Tools

Open Developer Tools and inspect the <head> section of the page.

Method 3 — SEO Crawling Tools

SEO crawling tools can be used to identify missing, duplicate, incorrect or conflicting canonical URLs across a website.


Frequently Asked Questions

1. Is canonical a redirect?

No. A canonical tag is a signal for search engines. It does not redirect the visitor.


2. Can dynamic CodeIgniter pages use canonical URLs?

Yes. CodeIgniter can generate canonical URLs dynamically from database records, slugs or route parameters.


3. Should every page have a canonical tag?

Many websites use self-referencing canonicals on their indexable pages, but the correct implementation depends on the site's URL structure and duplicate-content situation.


4. Can canonical tags solve duplicate content completely?

Canonicalization helps search engines understand preferred URL versions, but it should be part of a broader URL and technical SEO strategy.


5. Should canonical point to the homepage?

No. A page should generally canonicalize to its own preferred equivalent URL, not automatically to the homepage.


Conclusion

Canonical URLs are an important part of technical SEO for dynamic CodeIgniter websites. When the same or substantially similar content is accessible through multiple URLs, a properly implemented canonical tag can help search engines understand your preferred URL.

For a CodeIgniter website, canonical URLs can be generated dynamically using database records, URL slugs and controller parameters.

The most important thing is to make sure that your canonical URL is the correct preferred version of the page and that your overall URL, redirect and internal-linking structure is consistent.

Key Takeaway: Use canonical tags to clearly communicate your preferred URL version to search engines, especially when your CodeIgniter application generates multiple URLs for similar content.