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">
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
| Canonical | 301 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. |
When Should You Use a Canonical Tag?
Canonical URLs are useful in situations such as:
- Duplicate or substantially similar pages.
- Product pages with tracking parameters.
- Sorting and filtering URLs.
- Dynamic URLs containing query parameters.
- Multiple URL variations for the same content.
- 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);
}
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
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.
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.
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
- Using an incorrect or broken canonical URL.
- Using HTTP when the website uses HTTPS.
- Pointing every page to the homepage.
- Creating canonical URLs that redirect to another URL.
- Using different canonical URLs for equivalent page versions without a clear reason.
- Using canonical tags instead of 301 redirects when a page has permanently moved.
- Generating canonical URLs containing unnecessary tracking parameters.
- 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.
Related SEO Articles
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.