URL Slugs |
[eluser]brian88[/eluser]
In my database, I have a field called 'slugs' that I manually made for several category pages. example: my-awesome-category currently I have, site.com/controller/method/3 I want, site.com/controller/method/my-awesome-category I saw the documentation but I don't understand their example route they give: $route['product/:num'] = "catalog/product_lookup"; or how to use it for my example...I suppose it would be something like: $route['controller/method/:num'] = "controller/method/my-awesome-category" Also how would you make this so slugs are unique. site.com/controller/method/3/my-awesome-category
[eluser]JoostV[/eluser]
Basically you have two good options: 1. if your site does not have a lot of categories, just use the slug and query for that slug in the database. On saving a slug in your cms you need to check that it is unique, for instance by using the is_unique form validation rule. Just set up your urls like controller/method/unique-slug and retrieve the slug with $this->uri->segment(3); 2. if your site has a lot of categories you're better off including the id in your url. controller/method/3/unique-slug. Use the id to look up the record (again, the id is in $this-> uri->segment(3) ![]() In both cases you do not need to set any routing. You only need to set routing if segment 1 does not contain the controller name or segment 2 does not conatin the method name. For instance, if you wanted to direct such urls: /category/my-awesome-category to controller 'category' and method 'listing' you would set this routing: Code: $route['category/:any'] = 'category/listing/' . $1
[eluser]brian88[/eluser]
I like the 2nd way, site.com/controller/method/3/url-slug then i could just route it to be like site.com/url-slug right? So to add the url-slug to the end of the url i just add it as another parameter in my model function? function get_categories( $categoryID, $slug ) {} that right?
[eluser]JoostV[/eluser]
All you need to fetch the content is the id. The slug is only there for SEO purposes. What I would do to work with that is something like this (untested and typed on my phone ![]() Code: // Redirect if uri does not contain the right slug This way the page can only be accessed through exactly the right url, but the visitor is silently redirected to that url if he makes a typo in the slug.
[eluser]brian88[/eluser]
Let me see if I understand this... This is my controller function for the category views Code: public function category($catId) Im allowed to just add the slug to the end of the url like that? main/category/3/whatever-i-want-here and i wont get an error no matter what it is? Also my Model main_mod isnt accepting a parameter for getGenNav(). My category method gets the $catId Thanks a lot for your response, I plan on watching codeigniter.tv also!
[eluser]JoostV[/eluser]
There is indeed an error in the code: Code: // This is wrong: redirect($current_uri); Should be: You can add on what you want, as long as you don't use invalid characters. To prevent this, wrap your slug in url_title() when you create a link to a category. Code: echo anchor('main/category' . $id . '/' . url_title($slug), $category_name); You may want to read up on about URI handling in the manual ![]()
[eluser]brian88[/eluser]
I didn't realize you can put whatever you want in the url and it doesnt break after the id. i just tried mysite.com/controller/method/3/slug-qweqwererw-wrwertdgffhghgfd-bvczxdfg and it doesnt break So then can i route to mysite.com/slug-qweqwererw-wrwertdgffhghgfd-bvczxdfg instead? As long as that slugs in my database? Code: $route['(:any)'] = "controller/method/(:num)/$1"; So whats the point of Code: if ($current_uri != $proper_uri) {}
[eluser]JoostV[/eluser]
The '(:num)' bit should go out of your route there ![]() The fun thing about routing is that you can route anything anywhere. If you use controller/method/num/slug urls you do not need to set routing. If you wish to use category/123/this-is-a-slug urls you set: Code: // In this route the second segment HAS to be a number For c-123/this-is-a-slug urls you set Code: $route['^c-(.+)$'] = "controller/method/$1"; From a technical point of view it is not necessary to redirect. The point in the redirect is SEO. If the same content is present on multiple URLs, Google sees this as duplicate content. Your site can get penalized for that, meaning it'll drop in ranking.
[eluser]brian88[/eluser]
You shouldnt get duplicate content if i have the id in there right? every url will be unique. But, Thanks a lot. I think I got it now. I'm just gunna add the seo-slug at the end of the url. Then route it like site.com/123/seo-slug
[eluser]JoostV[/eluser]
Yes you can. Duplicate content is when the same content is available on multiple urls. category/1/some-slug category/1/oops-i-renamed-my-category category/1/you-know-what-i-ll-just-rename-my-category-again Google penalizes duplicate content. Of course Google would have to know about those urls. But it will when your administrator decides to rename a category after Google has already indexed it. Have a look at this example and try to rename the segment 'vader-schutter-toulouse-klaagt-politie': http://www.nu.nl/buitenland/2774884/vade...litie.html That's exactly the solution I mean. CNN and fox news do it differently. They just throw a 404 if you don't get the slug right. Personally, I think redirecting to the proper page is much more user friendly. |
Welcome Guest, Not a member yet? Register Sign In |