0
votes

I had some troubles with CodeIgniter routing: I want to build a site for showing some products, and need to build a URL follow the rule:

http://localhost/testsite/category-name/product-name/id.html or http://localhost/testsite/pro-id/category-name/sub-category-name1/.../product-name.html, with "category-name","sub-category-name1", "sub-category-name2"...,"product-name", and "id" were loaded dynamic from database.

How can I config the route value for this one? I've tried with some cases but it's not working:

$route['pro-(:num)/(:any).html'] = 'product/detail?pid=$1';
$route['(:any)/(:num)'] = 'product/detail?pid=$1';

Many thanks for any helps!

Thanks @Bira for your support, but the problem was still there :)

Here is the content of my 'routes.php'

$route['default_controller'] = 'product';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['home.html'] = 'product/index';
$route['pro-(:num)/(.+).html'] = 'product/detail?pid=$1';
$route['(:any)/(:any)/(:num).html'] = 'product/detail?pid=$3';

And the content of .htaccess

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^home.html  index.php/product/index
RewriteRule ^pro-([0-9]+)/(.*).html  index.php/product/detail?pid=$1
RewriteRule ^([0-9A-Za-z]+)/([0-9A-Za-z]+)/([0-9]+).html  index.php/product/detail?pid=$3

And ... only one URL is working "http://localhost/testsite/home.html" (the first rule). Other rules will return error: 404 Page Not Found (Ex: "http://localhost/testsite/pro-3/category-name/product-name.html")

Here is my test controller class:

class Product extends CI_Controller {
    public function index()
    {
        $this->load->view('products');
    }
    public function detail()
    {
        $pid = $_GET['pid'];
        $data['pid'] = $pid;
        $this->load->view('product_detail', $data);
    }
}

Thanks you so much for any supports, I'm just a newbie on CI :)

2
Thanks you, @Sparky, for making my question more clearly :) - huynhtuanh

2 Answers

1
votes

Make sure Apache .htaccess is enabled. Make sure the Apache module mod_rewrite is enabled.

1
votes

Remove pid=$1 from your route rules. It will work. See below code.

$route['pro-(:num)/(:any).html'] = 'product/detail';
$route['(:any)/(:num)'] = 'product/detail';