使用路由和重定向来防止相同内容上的重复链接


Using routing and redirect for prevent duplication links on the same content

假设我们有一个控制器

class Test 
{
   public function index()
   {
     echo 'hello!';
   }
} 

所以我可以把它命名为test/index或者为最佳实践设置路由像$routing['test'] = 'test/index';

所以现在我可以用两种方式调用它test或test/index和相同的页面有两个链接!我想知道这种情况下的最佳解决方案是什么?

所有我想也许检查索引,如果我在段'index'所以重定向到路由只是'test'。

我和Codeigniter一起工作,但我认为这个问题也是SEO特有的。

Summary:如果我使用路由,防止相同内容的双链接的最好方法是什么?

谢谢!

墙叫.htaccess规则阻止你。试试这样:

#instead of test put name of your default controller
RewriteRule ^(test(/index)?|index('.php)?)/?$ / [L,R=301]
RewriteRule ^(.*)/index/?$ $1 [L,R=301]

我在这个页面上找到了这段很棒的代码,在这里你可以看到基本的但广泛的.htaccess文件,用于CI制作的网站/应用程序。