codeigniter 301 redirect with router for old url and remaning no redirect

10,587

I was faced with a very similar situation recently. To save the 404 from all the results at Google & linked sites, I kept the old route but pointed it at a new controller/function. Theis function manages the the 301 redirection.

    public function oldurl_to_newurl()
    {
          $newURL = LogicToGetNewURL
          redirect($newURL,'location',301);
    }

Using the segments from the old URL, I query the database(if needed) and create the new URL. It then redirects the users accordingly. This was I am also able to track the some metrics for my analytic.

Share:
10,587
Salim Mujawar
Author by

Salim Mujawar

PERSONAL I’m Salim Mujawar. I currently live in Mumbai, India. with my wife and a kid. When I’m not working, I enjoy playing Guitar, watching movies, playing cricket, playing with my son and just hanging out with friends and family. PROFESSIONAL I have completed My Diploma & B.E ( Bachelor in Engineering ) in Computer. I am creating apps since 2010 and also in the mean time coding in PHP, but I do like to explore other languages. Currently my focus is mainly on web and mobile development, also exploring languages such as ruby and node.js. I love the web development community and would contribute as much as I can. SKILLZ PHP – Usually using CodeIgniter, CakePHP. Ruby – I’ve recently started delving into Ruby to. Python – Also delving in python. Javascript – Node.js, jQuery. Search – Solr, Elastic-Search Caching – Memcache NoSQL - Redies Message Broker – RabbitMQ Design – None at all.

Updated on June 04, 2022

Comments

  • Salim Mujawar
    Salim Mujawar almost 2 years

    I have a codeigniter site, for which i am doing SEO, so the issue is most of the urls are cached in search engines, what my old url is

    /product/details/productname/productid
    

    My new urls will be

    /tours/cityname/productname
    

    I want for my old urls to be redirected to new with 301, and new product url not to be redirected with 301 as they are new and will have the same url structure, as I search on codeigniter site about url routing in router.php, which gives an example for redirection as

    $route['product/(:num)'] = "catalog/product_lookup_by_id/$1";
    

    I think that this where i can make the change but how can i do a 301 in this pattern, by using header or is there a better way of doing this ?