在 CodeIgniter 中重新路由页面时如何防止重复内容?
假设我有一个控制器“Articles”,但我希望它显示为子文件夹(例如“blog/articles”),我可以添加这样的路线:
$route['blog/articles'] = 'articles';
$route['blog/articles/(:any)'] = 'articles/$1';
这工作正常,现在唯一的问题是 example.com/articles
和 example.com/blog/articles
都使用 Articles 控制器,因此解析为相同的内容。有办法防止这种情况吗?
为了防止人们不理解,增加一点清晰度:
- 在这个例子中,我没有“博客”控制器,但我希望“文章”等出现在该子文件夹中(这是组织的事情)。
- 我可以有一个带有“文章”功能的博客控制器,但我可能有一堆“子控制器”并且想要分离功能(否则我最终可能会为博客控制器中的单独实体提供 30 多个功能) 。
- 我希望 example.com/articles 返回 404,因为这不是正确的 URL,example.com/blog/articles 才是。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
将其放入您的控制器中:
Shove this in your controller:
您可以在 Codeigniter 控制器中使用子文件夹,因此在 CI 中,以下目录结构有效:
application/controllers/blog/articles.php 然后访问
http://example.com/blog/articles/*。
You can use subfolders in Codeigniter controllers, so in CI, the following directory structure works:
application/controllers/blog/articles.php and is then accessed at
http://example.com/blog/articles/*.
如果由于某种原因,您设置为路由而不是访问文件夹中的控制器(例如,您想要一个博客控制器,并且不想路由到它),则可以按照上面的建议进行操作并添加对构造函数的“博客”测试。
如果您使用的是 PHP5,则可以使用这样的构造函数:
或者,在 PHP4 中,可以使用这样的构造函数:
我建议使用
redirect('blog/articles')
而不是show_404( )
,不过,这样您就可以将点击 /articles 的用户引导到正确的位置,而不是仅仅向他们显示 404 页面。If, for some reason, you're set on routing instead of accessing the controllers in folders (you want to have a blog controller, for example, and don't want to route to it), you can do as suggested above and add the test for 'blog' to the constructor.
If you're in PHP5, you can use the constructor function like this:
or, in PHP4, like this:
I would suggest using
redirect('blog/articles')
instead ofshow_404()
, though, so that you're directing users who hit /articles to the correct location, instead of just showing them a 404 page.路由到那里并不意味着它将使用不同的控制器,它只是为同一控制器创建别名 url 段。如果您希望为这些 url 段使用不同的控制器,方法是创建另一个控制器。
Routing there does not mean it will use a different controller, it just creates alias url segment to same controller. The way will be to create another controller if you are looking to use a different controller for those url segments.
如果 /blog/ 和 /articles/ 使用相同的控制器,您只需在路由文件中添加新规则即可将其中一个控制器重新路由到另一个控制器。
If both /blog/ and /articles/ use the same controller, you can reroute one of them to a different one by just adding a new rule in your routes file.