Welcome Guest, Not a member yet? Register   Sign In
URL Slugs
#1

[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
#2

[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)Wink. You may also want to check if the proper slug is in the url and redirect the visitor to the proper url if it's not.

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
#3

[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?
#4

[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 Wink)
Code:
// Redirect if uri does not contain the right slug
// to avoid duplicate content penalties from Google
$cat = $this->categories->get_category($this->uri->segment(3));
$proper_uri = '/categories/listing/' . $cat->id . '/' . $cat->slug;
if ($uri_string() != $proper_uri) {
    redirect($proper_uri);
}

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.
#5

[eluser]brian88[/eluser]
Let me see if I understand this...
This is my controller function for the category views

Code:
public function category($catId)
{
     $data = array(
         // models
         'jokes' => $this->main_mod->getJokesByCat($catId), // get jokes my category
         'genNav' => $this->main_mod->getGenNav(), //get general categories
         'topicNav' => $this->main_mod->getTopicNav(), //get topic categories
        
         // main content
         'main_content' => 'category_view'
     ); // end data array

        /* your code
        $cat = $this->categories->get_category($this->uri->segment(3));
        $proper_uri = '/categories/listing/' . $cat->id . '/' . $cat->slug;
        if ($uri_string() != $proper_uri) {
             redirect($proper_uri);
        } */ end your code

        // my attempt
        $cat = $this->main_mod->getGenNav( $this->uri->segment(3) );
        $proper_uri = '/main/category/' . $cat->id . '/' . $cat->slug;

        // uri_string() returns: main/category/3
        $current_uri = uri_string();
        if ($current_uri != $proper_uri) {
             redirect($current_uri);
        }
        // dont we want them to redirect to main/category/3 ($current_uri)
        // if main/category/3/my-url-slug ($proper_url) is typed wrong?

        $this->load->view('main/cat_template.php', $data);
}

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!
#6

[eluser]JoostV[/eluser]
There is indeed an error in the code:
Code:
// This is wrong: redirect($current_uri); Should be:
redirect($proper_uri);

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 Wink
#7

[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) {}
? if you can put whatever you want in the slug as long as it has the id in there it wont break
#8

[eluser]JoostV[/eluser]
The '(:num)' bit should go out of your route there Wink

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
$route['category/:num/:any'] = "controller/method/$1/$2";  

// In this route, also urls like category/foo wil be routed to controller/method/$1
$route['category/:any'] = "controller/method/$1";

For c-123/this-is-a-slug urls you set
Code:
$route['^c-(.+)$'] = "controller/method/$1";
Etc.

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.
#9

[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
#10

[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.




Theme © iAndrew 2016 - Forum software by © MyBB