CodeIgniter, Pagination and SEO… How to NoIndex CI pagination.

After building a site using CodeIgniter’s pagination library, our SEO team let us know that search engines was spidering deep down our pagination, and all those pages were diluting the link juice of our site.  The other problem is that our html title tag was identical for the search queries, no matter how deep into the pagination the visitor was.   While my first inclination was to add some details to the page title ( Search for <term> |  15-30 out of 300 | <sitename>), the SEO crew suggested we simply rel=”noindex” the link, to keep the deep pages out of search engine index.

Looking into the pagination class on CodeIgniter  (1.7.2), it became obvious that, while you can customize quite a bit of the html output of the pagination, you can’t add anything inside the anchor link for the pagination system.   So, it was time to override a base CodeIgniter library again.

Make a file inside your system/application/libraries directory called MY_Pagination.php, and put the wrappers at the top like this:

class MY_Pagination extends CI_Pagination {
var $link_extracode            = ‘rel="noindex"’;

This makes a new class, that extends the built in Pagination that ships with CodeIgniter. The first thing to do is to add a new variable that will hold our html that we are putting in the links. Whatever is in your var $link_extradata will be the default text that goes in the link, but this will be changeable in your controllers when you call the pagination. Just like any other pagination config item, you can change it and initialize the pagination system from scratch again:

$config[‘link_extracode’] = ‘rel="noindex,nofollow"’;

This is standard of the pagination library, so you can refer to the documentation on this. We’re just creating a new variable to be used.

Now, open up the CodeIgniter’s Pagination Class, and copy the whole create_links() function (in 1.7.2, that’s line 103 – 239) and paste it into your new MY_Pagination.php file, replacing the /* CODE GOES HERE */ line.

Next, we need to edit our version of create_links() function with the added functionality. You’ll see, near the end of the create_links function a line (mine is on line 92 of my edited file), that looks like this:
$output .= $this->first_tag_open.'<a href="’.$this->base_url.’">’.$this->first_link.'</a>’.$this->first_tag_close;
We just need to add our new variable, $this->link_extracode, into the link, like this:
$output .= $this->first_tag_open.'<a ‘. $this->link_extracode .’ href="’.$this->base_url.’">’.$this->first_link.'</a>’.$this->first_tag_close;

That takes care of the first_tag, you’ll have to add the $this->link_extracode to 3 more anchor tags below this (line 100, 117, 125 and 131 in my verison of the file). Once all the anchor html tags have our custom code in them, and you save the file, you can then set any text inside the anchor via the default setting at the top of the MY_Pagination file, or in your controller via the $config option.

You can use this technique to add anything you like to the link, not just rel=’noindex’ as I have in this example. Drop me a comment below, and let me know what you are using it for.