user1822748
user1822748

Reputation: 39

How can I shorten routes in Codeigniter for certain requests?

I have a page that has this category URL website.com/category/view/honda-red-car and I just want it to say http://website.com/honda-red-car no html or php and get rid of the category view in the URL.. this website has been done using the CodeIgniter framework..

also this product view URL website.com/product/details/13/honda-accord-red-car and I want it to be website.com/honda-accord-red-car PLEASE HELP!!!

I cannot find correct instructions on what I am doing wrong??

Upvotes: 0

Views: 1158

Answers (2)

complex857
complex857

Reputation: 20753

Take a look into the URI Routing part of the user guide.

If you have concrete set of urls that you want to route then by adding rules to the application/config/routes.php you should be able to achieve what you want.

If you want some general solution (any uri segment can be a product/details page) then you might need to add every other url explicitly to the routes.php config file and set up a catch-all rule to route everything else to the right controller/method. Remember to handle 404 urls too!

Examples:
Lets say the /honda-red-car is something special and you want only this one to be redirected internally you write:

$routes['honda-red-car'] = 'product/details/13/honda-accord-red-car';

If you want to generalize everything that starts with the honda- string you do:

$routes['(honda-.*)'] = 'product/details_by_slug/$1'; // imaginary endpoint

These rules are used inside a preg_replace() call passing in the key as the pattern, and the value as the replace string, so the () are for capture groups, $1 for placing the capture part.

Be careful with the patterns, if they are too general they might catch every request coming in, so:

$routes['(.*)'] = 'product/details_by_slug/$1';

While it would certainly work for any car name like suzuki-swift-car too it would catch the ordinary root url, or the product/details/42 request too.

These rules are evaulated top to bottom, so start with specific rules at the top and leave general rules at the end of the file.

Upvotes: 2

user1157393
user1157393

Reputation:

In Routes.php you need to create one like so

$route['mycar'] = "controller_name/function_name";

So for your example it would be:

$route['honda-red-car] = "category/view/honda-red-car";

Upvotes: 2

Related Questions