代码之家  ›  专栏  ›  技术社区  ›  therufa

如何将每个URI调用重定向到一个控制器,除了一些静态的控制器?

  •  1
  • therufa  · 技术社区  · 14 年前

    我使用的是codeigner,我想让我的门户网站对SEO更友好一点。 我有一个控制器(文章),它处理门户上的每一篇文章。 URL如下所示:

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

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

    我也想隐藏文章,但是如果我隐藏了它,每次调用都会转到文章控制器,这不是我想要的,因为我希望我的URL如下所示:

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

    我在routes.php中尝试了使用regexp路由规则,但是我没有找到正确的方法。

    4 回复  |  直到 11 年前
        1
  •  3
  •   Community CDub    7 年前

    几天前我对这个问题的回答相当广泛:

    How to get an array of all controllers in a Codeigniter project?

        2
  •  3
  •   bschaeffer    14 年前

    使用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 加入你的网址,这样做:

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

    你仍然需要创建 类别 不过,您的控制器中的方法。

        3
  •  2
  •   therufa    14 年前

    好啊!问题解决了!

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

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

    这一行将每个非控制器请求返回到我的文章控制器,因此我有我想要的URL:)

        4
  •  0
  •   Artefacto    14 年前
    RewriteCond %{REQUEST_URI} !^articles(?:/|$)
    RewriteCond %{REQUEST_URI} !^static1(?:/|$)
    RewriteCond %{REQUEST_URI} !^static2(?:/|$)
    ...
    RewriteRule ^/(.*) /articles/$1 [QSA,NE]