Farbour
Farbour

Reputation: 171

CodeIgniter - How Do I Rewrite URLs with URL Parameters

I need to turn URL's such as the following:

/catalog/products/24

into cleaner URL's such as the following:

/catalog/product-name-here

I'm using CodeIgniter, and rewrite engine is on and setup. I also setup rewriting in the routes.php file as follows:

$route['catalog/products/(:any)'] = 'catalog/view/$1';

Question: Where do I replace the product ID /24 with /product-name-here? I can successfully get the parameter $1, and fetch the product name. I can't seem to figure out how to rewrite the URL with this dynamic value though. Thanks in advance!

Upvotes: 2

Views: 2381

Answers (2)

user193309
user193309

Reputation:

Just store the products name as a URL slug into your product row in the database table. Good practice for that the url_title() method from the URL Helper.

Then you just have to search for the slug from your database in your code, rather than the id.

So this is just a hint because I don't have the time right now. But if you've got questions, ask it here, I will come back to answer it.

And sorry for my poor English! :)

Upvotes: 0

Ben James
Ben James

Reputation: 125167

I'm not a codeigniter user but I smell that URL rewriting is not the best place to achieve this.

You still need to write a controller method that can identify a product using something like slug='product-name-here' instead of id=24.

Upvotes: 2

Related Questions