How to prevent duplication of content when re-routing pages in CodeIgniter?

Say I have an “Articles” controller, but I want it to appear as a subfolder (for example, “blog / articles”), I can add a route as follows:

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

This works great, the only problem is that example.com/articlesthey example.com/blog/articlesuse the article controller and thus allow the same content. Is there any way to prevent this?

To add a bit more clarity in case people don't understand:

  • In this example, I don’t have a “blog” controller, but I want “articles”, etc. displayed in this subfolder (this is organization).
  • I may have a blog controller with an “article” function, but I will probably have a bunch of “subcontrollers” and you want to separate the functionality (otherwise I could get 30+ functions for individual objects in the blog controller).
  • I want example.com/articles404 to be returned, as this is not a valid URL, example.com/blog/articlesthere is.
+3
source share
5 answers

Move this to your controller:

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

    $this->uri->uri_segment(1) == 'blog' OR show_404();
}
+6
source

You can use subfolders in Codeigniter controllers, so the following directory structure works in CI: applications / controllers / blog / articles.php, and then http://example.com/blog/articles/ *.

+2

- , (, , ), , "" .

PHP5, :

function __construct()
{
    parent::Controller();
    $this->uri->uri_segment(1) == 'blog' OR redirect('/blog/articles');
}

, PHP4, :

function Articles()
{
    parent::Controller();
    $this->uri->uri_segment(1) == 'blog' OR redirect('/blog/articles');
}

redirect('blog/articles') show_404(), , , / , , 404.

+1

Routing there does not mean that it will use a different controller, it simply creates a segment of the alias URL for the same controller. The path will be to create another controller if you want to use a different controller for these URL segments.

0
source

If both / blog / and / articles / use the same controller, you can redirect one of them to the other by simply adding a new rule to your routes file.

0
source

Source: https://habr.com/ru/post/1753691/


All Articles