Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I prevent duplicate content when re-routing pages in CodeIgniter?

Say I have a controller, "Articles" but I want it to appear as a sub-folder (e.g. "blog/articles"), I can add a route like this:

$route['blog/articles'] = 'articles';
$route['blog/articles/(:any)'] = 'articles/$1';

This works fine, the only problem now is that example.com/articles and example.com/blog/articles both use the Articles controller and thus resolve to the same content. Is there a way to prevent this?

To add a little more clarity in case people aren't understanding:

  • In this example, I don't have a 'blog' controller, but I want 'articles' etc to appear to be in that subfolder (it's an organization thing).
  • I could have a blog controller with an 'articles' function, but I'm likely to have a bunch of 'subcontrollers' and want to separate the functionality (otherwise I could end up with 30+ functions for separate entities in the blog controller).
  • I want example.com/articles to return a 404 since that is not the correct URL, example.com/blog/articles is.
like image 652
DisgruntledGoat Avatar asked Nov 30 '25 06:11

DisgruntledGoat


1 Answers

Shove this in your controller:

function __construct()
{
    parent::Controller();

    $this->uri->uri_segment(1) == 'blog' OR show_404();
}
like image 84
Phil Sturgeon Avatar answered Dec 03 '25 23:12

Phil Sturgeon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!