CodeIgniter Forums
Rewrite url or use routing - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Rewrite url or use routing (/thread-49770.html)



Rewrite url or use routing - El Forum - 03-02-2012

[eluser]0v3rth3d4wn[/eluser]
Hi! In my application I have the following url: http://mysite.com/bg/category/index/2/12 where category is my controller, index is the method and 2 and 12 are the parameters. 2 is the category id and 12 is the subcategory id. How can I get rid of the index (I tried with routing but no luck) and how can I replace or add the name of the category and the subcategory depending on the id which is the id in the database. I want to achieve something like http://mysite.com/bg/category/clothing/shirts . I'm not sure what I have to use - routing or some .htaccess magic... Thanks.


Rewrite url or use routing - El Forum - 03-02-2012

[eluser]PhilTem[/eluser]
Use routing. Or _remap()-functionality.

Drop off your routes.php code here so we can debug it. In most cases it's just a typo preventing the routes to really work. And please use the [ code ]-tag - without white spaces of course.


Rewrite url or use routing - El Forum - 03-02-2012

[eluser]0v3rth3d4wn[/eluser]
Here is my route.php
Code:
$route['default_controller'] = "home";
$route['404_override'] = '';

// URI like '/eng/about' -> use controller 'about'
$route['^(bg|eng)/(.+)$'] = "$2";

// '/eng' and '/bg' URIs -> use default controller
$route['^(bg|eng)$'] = $route['default_controller'];  

//here first I try to get rid of the index but something isn't just right and it is not working...
$route['(bg|eng)/category/:num/:num'] = "category/index/$3/$4";



Rewrite url or use routing - El Forum - 03-02-2012

[eluser]Aken[/eluser]
Your routes should go in order from most to least specific, and always put custom routes after the default and 404 routes.

Try putting your last route after the 404_override.


Rewrite url or use routing - El Forum - 03-02-2012

[eluser]0v3rth3d4wn[/eluser]
Thanks for the tip, but I still can't get my route to work. I'm not writing it right.


Rewrite url or use routing - El Forum - 03-02-2012

[eluser]Aken[/eluser]
You also need parentheses around :num in order to use the $1/$2/... back references.


Rewrite url or use routing - El Forum - 03-03-2012

[eluser]PhilTem[/eluser]
To sum up previous posts, your routes.php should look like this

Code:
$route['default_controller'] = "home";
$route['404_override'] = '';

//here first I try to get rid of the index but something isn't just right and it is not working...
$route['(bg|eng)/category/(:num)/(:num)'] = "category/index/$3/$4";

// URI like '/eng/about' -> use controller 'about'
$route['^(bg|eng)/(.+)$'] = "$2";

// '/eng' and '/bg' URIs -> use default controller
$route['^(bg|eng)$'] = $route['default_controller'];

If you don't wanna have numeric parts within the /category/-uri, then replace it with either .+ or (:any) and make your category/index-method to translate the given string to a category- or sub-category id respectively.

Code:
//here first I try to get rid of the index but something isn't just right and it is not working...
$route['(bg|eng)/category/(:any)/(:any)'] = "category/index/$3/$4";



Rewrite url or use routing - El Forum - 03-03-2012

[eluser]0v3rth3d4wn[/eluser]
Thank you all for the help and the tips, everything is now working well.
As for the removing the id-s in the url and replacing them with let's say the category name, the only thing that comes to my mind is using an extra unique field for each category(subcategory, product) entry. Let's say I have category with id:1 and name:clothing. I should create a field which will be also "clothing" and will be unique and I will use it as my link instead of the id.
If I am to have two categories with the same name(which won't happen but in the products it may) I should just check if such product exists and if it does I should add the same name with "-1" at the end of the string. I think this is something like a permalink or I'm wrong? Is this a good way to go? Thanks again!