How to make “truly” SEO friendly URLs in CodeIgniter (without “duplicate content” on underscore URLs)

I promised myself I won’t do anymore tutorials regarding CodeIgniter until a new version is about to appear, but I can’t help myself. And this subject seems more than appropriate for a new tutorial.

Let us start with the beginning. In routes.php (application/config/routes.php), we have a configuration parameter called “translate_uri_dashes“. If we set this parameter to TRUE, the CodeIgniter framework will translate the dashes (“-“) in our urls into underscores (“_”) when calling a controller or a method. This, of course is what we want in order to have “SEO friendly” URLs (although I think it’s strange that up until this moment, search engines can’t interpret an url).

That means that when the CodeIgniter sees an address like will know to call the Test_controller method and the a_url_method() method.

But, hey!… This won’t remove the possibility of accessing the same controller and method by using underscores in the url. So, that means that you get same content at and at and that is a BIIIG “NO-NO” from our beloved search engines. Why? The famous “DUPLICATE CONTENT“.

Then how do save ourselves from this SEO Armageddon? Well… Let us take a look at the Router.php inside the system/core. Looking at that we will see that the method that deals with “translating” the URL into a controller and its method is _set_request():

So, this is the culprit… But what now? Should we simply modify it? No… NEVER MODIFY THE SYSTEM FILES IN CODEIGNITER!

Let’s go into our application/core and create a file named MY_Router.php in it. As you may, or may not know, CodeIgniter automatically extends anything that starts with MY_… This class will simply extend the core Router.php and we will only modify the method in question:


As you can see in lines 35-38 and 41-44 we look to see if what we get from URL contains any underscores (“_”). If it does, then that means that the URL is no good and we need to do a redirect to the url that only contains dashes “-“. Now in lines 51-57 we simply do a redirect with a 301 server code (moved permanently), if either the controller’ or the method’s URL contain underscore “_”.

And that’s it. Of course, this is not an universal solution and is prone to errors if you have a more “exotic” setup of urls. Do you have another solution? I would be more than happy to put it here. Can someone, please, buy me a coffee (hint-hint)?

0 thoughts on “How to make “truly” SEO friendly URLs in CodeIgniter (without “duplicate content” on underscore URLs)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

No spam? * Time limit is exhausted. Please reload CAPTCHA.