This is what I ended up doing
Enabled query strings for pagination and changed the parameter that contains the page number (by default it is set to 'per_page')
$config['page_query_string'] = TRUE;
$config['query_string_segment'] = 'page';
Set the base URL for the pagination links
$config['base_url'] = base_url().'/products/'.$data['category']->slug.'/'.$data['sub-category']->slug;
I use additional parameters which I preserve in the pagination URLs, this is to avoid recursively including the page parameter. Also it will keep the parameters for the first_url which by default will have none.
$query_string = $_GET;
if (isset($query_string['page']))
{
unset($query_string['page']);
}
if (count($query_string) > 0)
{
$config['suffix'] = '&' . http_build_query($query_string, '', "&");
$config['first_url'] = $config['base_url'] . '?' . http_build_query($query_string, '', "&");
}
Set the rest of the pagination config
$config['per_page'] = $per_page;
$config['uri_segment'] = '4';
$config['use_page_numbers'] = TRUE;
$config['num_links'] = '3';
$config['prev_link'] = '‹';
$config['next_link'] = '›';
$config['first_link'] = '«';
$config['last_link'] = '»';
$config['total_rows'] = $data['products_total'];
Optional Modify / extend the pagination library to use '?' instead of '&'.
Replace this line
$this->base_url = rtrim($this->base_url).'&'.$this->query_string_segment.'=';
With this line
$this->base_url = rtrim($this->base_url).'?'.$this->query_string_segment.'=';
This works in this case because I know 'page' will always be the first parameter as I'm not using enable_query_strings
.
If you don't want to modify / extend the pagination library, you will have to add the '?' to the $config['base_url']
. The pagination URLs will have both '?' and '&' like '?&page=6'.
Sources