1

我正在使用 codeigniter 并希望使我的门户网站对 SEO 更加友好。我有一个控制器(文章)来处理我门户网站上的每篇文章。URL 如下所示:

example.com/articles/category-sub-category/article-name

我正在使用 mod rewrite 模块来隐藏我的 index.php,并使用 codeigniter 路由来隐藏处理每个调用的控制器操作。

我也想隐藏文章,但如果我隐藏它,每个调用都会转到文章控制器,这不是我想要的,因为我希望我的 URL 看起来像这样:

example.com/category-sub-category/article-name

我已经在 routes.php 中使用正则表达式路由规则进行了尝试,但我发现没有办法让它正确。

4

4 回答 4

3

使用 CI 的路由功能,您必须像这样为每个类别设置一个路由..

$route['category_one/:any'] = 'articles/category/category_one';
$route['category_two/:any'] = 'articles/category/category_two';
//.. and on and on until you've routed them all

您必须category在控制器中有一个方法,Articles否则您还必须为每个类别创建一个方法,这将失控。

至少使用 CodeIgniter,您最好将articles部分保留在您的 url 中并这样做:

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

不过,您仍然需要category在控制器中创建该方法。

于 2010-05-14T18:47:43.043 回答
3

几天前,我非常广泛地回答了这个问题:

如何在 Codeigniter 项目中获取所有控制器的数组?

于 2010-05-16T22:05:45.897 回答
2

行!问题解决了!

我在以下网站上找到了我的问题的解决方案:http: //bizwidgets.biz/web-design/codeigniter-routes-trick-removing-controller-names-from-the-uri-to-keep-urls-短的/

$route['^(?!account|about|showcase|etc).*'] = "articles/read/$0";

此行将每个非控制器请求返回到我的文章控制器,所以我有我想要的 url :)

于 2010-05-14T22:54:42.747 回答
0
RewriteCond %{REQUEST_URI} !^articles(?:/|$)
RewriteCond %{REQUEST_URI} !^static1(?:/|$)
RewriteCond %{REQUEST_URI} !^static2(?:/|$)
...
RewriteRule ^/(.*) /articles/$1 [QSA,NE]
于 2010-05-14T16:38:30.057 回答