简体   繁体   中英

Change Dynamic Url using htaccess in codeigniter

I have the following url structure: www.mysite.com/temporary/articles.php/artid=1

I would like to change it with: www.mysite.com/temporary/articles/article-title-here.

where article-title should be based on artid . Anyone can tell me how can I do that?

No .htaccess needed. I would create another controller method and add some routes. I would use the following approach:

  1. You submit the following URL: www.mysite.com/temporary/articles.php/artid=1
  2. CI catches it and reroutes it as such: In your routes.php:

     $route['temporary/articles.php/artid=(:any)'] = "temporary/articles/search_by_id/$1"; 
  3. In your Controller:

     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") } } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM