I am having difficulties with Codeigniter. I am trying to get data from a MySQL data by passing the record ID as part of the URL
The URL is to be
localhost/site_folder/page/page_title/2
In the above URL, page is the name of the controller and 2 is the primary ID of the record in the database (this could be any number from 1 to 9999).
My controller includes this:
public function index()
{
$this->load->helper('url');
$this->load->model('pages_model');
$id = $this->uri->segment(3,1);
if (empty($id))
{
show_404();
}
$data['page'] = $this->pages_model->get_page($id);
$this->load->view('page',$data);
}
My .htaccess contains this
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
When I enter localhost/site_folder/page/page_title/2
into the address bar, it throws a 404.
Yet, when I enter localhost/site_folder/page
it shows the default database entry as shown in the second value of segment(3,1)
above.
So, how should I change the .htaccess file for a workable rewrite?
I have tried the following, but none worked for me:
- RewriteRule .* page/$0 [PT,L]
- RewriteRule .* page/(*.)/$0 [PT,L]
- RewriteRule .* page/(?*.)/$ [PT,L]