Change Dynamic Url using htaccess in codeigniter

385 Views Asked by At

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?

1

There are 1 best solutions below

2
CodeGodie On

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