I am building a website with codeigniter and my urls follow this format: link1 where the id e.g. 123 is used to query the database for a record.
The last uri segment is just for SEO friendly purposes and can be changed to anything - e.g. link2
Following on from this answer, how can I redirect all variations of the url to display the correct text in the last segment?
Should I check the url is the correct format in my controller and redirect it there?
e.g.
if($this->uri->rsegment(4, 0) !== $this->myModel->recordName) {
redirect($id.'/'.$this->myModel->recordName);
}
Thanks
---UPDATE1---
Just found a problem with the above method. Sometimes I will have extra segments on the end of the urls - e.g. link - and this won't work with that
---UPDATE2---
Here is my latest code. This ignores additional segments and works if just the id is provided. Hoping someone can confirm if this is the right/best way to do it!
$correctRecordName = myEncodeForUrlFunction($this->myModel->recordName);
$rsegments = $this->uri->rsegment_array();
if(count($rsegments) < 4 || $rsegments[4] !== $correctRecordName) {
$rsegments[4] = $correctRecordName;
$url = site_url($rsegments);
redirect($url, 'location', 301);
}