当URL中的产品ID更改时,重定向url codeigniter

时间:2017-05-30 14:57:53

标签: php .htaccess codeigniter url-redirection

我的网址类似于base_url / Category / productname-254

这里254是该产品的ID。

此外,类别是通过在route.php中添加路由来实现的 $ route ['(:any)/(:any)'] =' controller / index / $ 1 / $ 1';

如果我将产品ID从254更改为产品ID 344,那么它应该重定向到344产品页面。

如下网址:

http://www.marketsandmarkets.com/Market-Reports/cardiac-monitoring-advanced-technologies-and-global-market-55.html

如果您将上述网址中的最后一个数字55更改为58,则会将其重定向到以下网址 http://www.marketsandmarkets.com/Market-Reports/nanomaterials-58.html

这是产品ID 58 的产品页面。

以下是我获取网址的函数:

function get_report_url($reporttitle,$reportid){
        //$title1 = str_replace(array( '(', ')' ), '',$reporttitle);
        $title1 = str_replace(array( '(', ')' ), '',$reporttitle);
        $title1 = str_replace(array( ',','?' ), "-",$title1);
        $title1 = str_replace(array( "'", "&", "º","°","ordm;" ), "",$title1);

        $title=explode(" ",$title1);
        //$title=explode(" ",$row['reportTitle']);
        $fintitle="";
        foreach($title as $words){
            $fintitle=$fintitle.$words.'-';
            }
        $fintitle=$fintitle.$reportid;  

        $query1 = $this->db->query("SELECT category FROM ur1 WHERE id=".$reportid);
        $cat = $query1->row();
        $url = base_url()."".$cat->category."/".$fintitle;
        //$url = base_url()."index/report/".$cat->category."/".$fintitle;
        return $url;
    }

控制器转到上面的网址:

class Index extends CI_Controller {
function __construct(){
    parent::__construct();
}

function report($cat,$rep){
    $ttl = explode("-",$this->uri->segment(2));
    $reportid = end($ttl);
    //echo "reportid  ".$reportid;
    /*echo "category ".$cat;*/
    //exit;
    $this->load->model('show_model','',TRUE);
    $arr_result = $this->show_model->get_report_details($reportid);
    //echo "reportid  ".$reportid;
    /*print_r($arr_result);
    exit;*/
    $data['error'] = $arr_result[0];
    $data['result'] = $arr_result[1];
    $this->load->view('includes/header');
    $this->load->view('includes/breadcrumbs');
    $this->load->view('pages/reportpage',$data);
    $this->load->view('includes/footer');
}

}

route.php

$route['(:any)/(:any)'] = 'index/report/$1/$1';

感谢任何帮助。 在此先感谢

1 个答案:

答案 0 :(得分:0)

//go to application->config->routes.php  

$route['404_override'] = 'Category'; //override error page with category controller 
class Category extends MY_Controller {        
 function index($product_id=''){   
   //get product data using product id and load data on view   
  }  
 }