All,
I am currently using CakePHP to completely revamp my current NEWS site. I have or will transfer all current articles to my new site and they will take on the same article_id as the current site. However, I have realized of the following problem.
My current site uses the following setup as URL:
http://www.mydomain.com/?c=140&a=4388
c=references the category ID and a=references the article ID. My new setup (CakePHP) I take advantage of slugs and now my articles URLs display as:
http://www.mydomain.com/noticia/this-is-an-article-slug
Since, I have noticed, thru webstats, that many of my articles are accessed thru links setup on other sites like Facebook, I thought it would be crucial to create a system/route that will take care of this issue for me.
This is what I am thinking:
- Create a route that detects requests similar to what is mentioned above
- Make the route pass the
avalue as parameter to a redirect function in my articles controller, such asarticleRedirect($article_id) -
This function would then look up in the database for
slugbased on the passed$article_idand redirect to the new address (See function below)// Function in articles controller to redirect old site's article url to // new websites url format function articleRedirect($article_id = NULL){ $this->Article->recursive = -1; $slug = $this->Article->findById($article_id); if($slug == NULL){ $this->Session->setFlash('Article not found'); $this->redirect('/noticias'); }else{ $this->redirect('/noticia/'.$slug['Article']['slug']); } }
I think that should work. However, I need some serious help with routing. Can anyone suggest a route I can use.
Thank you very much.
Thanks for Oldskool for the willingness to help. However, I had trouble implementing it. I decided to stay away from using .htaccess or routes and opted to implement a function in app_controller that will handle it for me as such:
In my
app_controller'sbeforeFilter()I added the following:Then in my
app_controller.phpI created the following function:If old url the above function will transfer to the articleRedirect function which will then redirect to the right document
It is probably not the best way to handle this kind of things this way since it makes a call to that function everytime time my site is accessed, but it is working for me without major problems.