在codeigniter中使用htaccess更改动态URL

时间:2015-07-08 12:00:56

标签: .htaccess codeigniter codeigniter-routing

我有以下网址结构: www.mysite.com/temporary/articles.php/artid=1

我想改为: www.mysite.com/temporary/articles/article-title-here。

其中文章标题应基于artid。 任何人都可以告诉我该怎么做?

1 个答案:

答案 0 :(得分:0)

不需要.htaccess。我会创建另一个控制器方法并添加一些路由。我会使用以下方法:

  1. 您提交以下网址:www.mysite.com/temporary/articles.php/artid=1
  2. CI抓住它并重新路由它: 在您的routes.php中:

    $route['temporary/articles.php/artid=(:any)'] = "temporary/articles/search_by_id/$1";
    
  3. 在您的控制器中:

    class Articles extends CI_Controller {
    
        public function index($title){
             //load your model
             //from your model, query your database to get your article info by title
             //send results to your view.
        }
    
        public function search_by_id($id){
             //load your model
             //from your model, query your database to get your article title by id. Set it = $title
             redirect("temporary/articles/$title")
        }
    
    
    
    }