Welcome Guest, Not a member yet? Register   Sign In
Best way to generate part of a URL that is just used for visuals/seo
#1

[eluser]Dannymx[/eluser]
What's the best way to create an url like this when the last segment is not present, in this url the last segment is generated by some data pulled from the database:

Code:
example.com/controller/function/id/some-description-just-for-visuals-and-seo

In this case, if the user just enters the id and whatever in the last segment, the page will be loaded anyway (because the result is based in the id segment).

So, what's the best way to ALWAYS get that last segment present?

It is ok if the controller checks if the segment is empty (or incomplete?) and then redirect with the full text in the url?
#2

[eluser]Developer13[/eluser]
Is "id" literally the letters "i" and "d", or is it a numeric key value? If it's not literally "id", you really don't need it there in the first place. You can store your some-description-just-for-visuals-and-seo as the 'url friendly slug' in your database along with the record it belongs to. You would then use the slug to pull the record from the database instead of the id.

As far as making sure a particular segment is present:

Code:
<?php if (!$this->uri->segment(n)) { redirect('somewhere_else'); } ?>

n would represent the number of the segment.
#3

[eluser]Dannymx[/eluser]
[quote author="Developer13" date="1311153512"]You can store your some-description-just-for-visuals-and-seo as the 'url friendly slug' in your database along with the record it belongs to. You would then use the slug to pull the record from the database instead of the id.[/quote]

Yes, but in my case sometimes the description segment (actually is going to be used for names) can repeat so that'll make conflict :-(
#4

[eluser]Developer13[/eluser]
Gotcha - in that case, you could still use the redirect logic.

Code:
<?php

if (!$this->uri->segment(4)) {
    
    $description = $this->mdl_something->get_something($this->uri->segment(3)); // retrieve the description from your db based on the id
    
    redirect('controller/function/id/' . $description); // redirect the user to the full url
    
}
?>
#5

[eluser]Dannymx[/eluser]
Oh well, then I guess I will end up using the redirect method anyway, thanks :-)

If anyone else has/knows a different method to achieve this, please let me know.
#6

[eluser]Aken[/eluser]
Redirects for this is silly, IMO.

First of all, if you URL structure is actually following the controller/function/parameter/parameter logic, your function will load regardless of how many parameters are in the URL.

If I have a controller "blog" with a function "view", I can enter this URL and that function will load properly:
Code:
http://mysite.com/blog/view/19/monkeys-are-weird/checkoutmy/123/hahaitjustkeepsgoing/cheeseiepoofs

The downside to that is obviously a URL with that type of directory structure doesn't exist. So in the off chance that you generate an incorrect URL, it will still be accessible, which can lead to odd search engine additions and things of that nature.

My suggestion, if you think it's necessary to avoid that possibility, is to create a set of routes to control what is available and what is not. These four rules, going from most specific to most generic, will prevent all URLs except 'controller/function/id/a-title-goes-here' from working:
Code:
$route['controller/function/:num/:any/:any'] = 'sandbox/notfound';
$route['controller/function/(:num)/:any'] = 'sandbox/blog/$1';
$route['controller/function/(:num)'] = 'sandbox/blog/$1';
$route['controller/function/:any'] = 'sandbox/notfound';

These routes most likely could be improved upon (you can use regular expressions to make things even more specific), but they work as a quick solution.
#7

[eluser]Developer13[/eluser]
Why force it to break and not load correctly if the required element (the id) is in the url but some other fluff isn't? This is why I went along with the redirect - of course you could do some fancy url rewriting as an alternative... but the simplest solution, I think, as to not disable it from loading - but to correct it instead.
#8

[eluser]Aken[/eluser]
The routes I set up allow for just the ID with no additional text fluff. If the user wants, they can use the controller to check if the fluff exists and redirect to the URL that includes it.
#9

[eluser]Dannymx[/eluser]
I don't think this has something to do with the routing, since what I want is always to have the description (the full description) in the last segment.

Example:

These 'incorrect' urls
Code:
example.com/controller/function/10/description-of-the-arti
example.com/controller/function/10/descripti
example.com/controller/function/10/

Always redirect to:
Code:
example.com/controller/function/10/description-of-the-article

The only solution so far now is the redirect, but maybe there are other solutions that I'm not aware of




Theme © iAndrew 2016 - Forum software by © MyBB