使用CodeIgniter重写url

使用CodeIgniter重写url,codeigniter,seo,codeigniter-routing,Codeigniter,Seo,Codeigniter Routing,我读了很多论坛主题,但没有找到任何符合我需要的答案 我正在运行一个博客系统,每篇文章的当前URL如下:www.example.com/controller/method/id。然而,对于每一篇文章,我都有一个存储在数据库中的标题,并且希望在url中使用该标题,因此它看起来是这样的:www.example.com/title您好,您需要大量的代码,而您自己没有做任何研究 最好的方法是将id与标题一起使用:http://www.example.com/id/long-title这比只使用标题要好得多

我读了很多论坛主题,但没有找到任何符合我需要的答案


我正在运行一个博客系统,每篇文章的当前URL如下:
www.example.com/controller/method/id
。然而,对于每一篇文章,我都有一个存储在数据库中的标题,并且希望在url中使用该标题,因此它看起来是这样的:
www.example.com/title

您好,您需要大量的代码,而您自己没有做任何研究

最好的方法是将id与标题一起使用:
http://www.example.com/id/long-title
这比只使用标题要好得多,因为:

  • 同一标题可以出现多次(产生可以避免的问题)
  • 由于使用slug而不是ID进行查询,加载/搜索速度较慢(性能较慢)
  • 用户需要记住整个标题(id+标题;用户可以复制部分损坏的url www.example.com/234593/此标题基于-/观点
  • 为了使我的提案生效,您需要:

    设置路由(application/config/routes.php)

    设置控制器(application/controllers/blog.php)

    现在,您可以使用(生成)url
    www.example.com/1
    (这将使用301头重定向到
    /1/title
    )或使用(生成)
    www.example.com/1/title
    链接,但是如果生成类似以下url的url:
    /1/假标题
    (标题对于id 1无效,它将不会重定向到正确的标题)


    此解决方案对SEO很友好。

    很抱歉,我明天会尝试,我现在正在进行另一个项目,我将更新主题。感谢您的帮助我已经尝试了您的解决方案,它可以工作,但我想删除url中的“/front/post/”是否可能?再次感谢。它的工作方式,你只需要生成这样的链接
    example.com/id/title
    example.com/id
    (重定向到
    example.com/id/title
    ),请重新阅读答案。
    //leave two CodeIgniter's routes on top
    $route['default_controller'] = "welcome";
    $route['404_override'] = '';
    
    //leave this two routes in this order + make them LAST routes
    $route['(:num)'] = 'blog/post/$1'; //if only id is in url
    $route['(:num)/(:any)'] = 'blog/post/$1/$2'; //if id and title is in url
    
    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    class Blog extends CI_Controller {
    
        public function __construct()
        {
            parent::__construct();
            //load models helpers etc if not autoloaded
            $this->load->helper('url');
    
        }
    
        public function index()
        {
            echo 'should throw 404 or redirect home because id was not provided';
    
        }
    
        public function post($id = null, $title = '') 
        {
            if (!$this->validId( $id )) redirect(); //redirect somewhere because id of post is invalid search/404/home...
            if (empty(trim($title))) {
                redirect(base_url().$id.'/'.$this->getTitle($id), 'location', 301); //redirect to same page search engines friendly (301 header)
            }
    
            //display what you need
    
            echo 'params; id: ' . $id . ' title: ' . $title;
    
        }
    
        private function validId($id) 
        {
            if (is_numeric($id))
                return true; //use database to check validity in this case every id is valid
        }
    
        private function getTitle() 
        {
            //id should be good to use at this point
            //get title using database
            return $this->seoUrl('How you react of crying babies will determine their future.');    //made up title 
        }
    
        private function seoUrl($string) 
        {
            //source: http://stackoverflow.com/a/11330527/1564365
            //Lower case everything
            $string = strtolower($string);
            //Make alphanumeric (removes all other characters)
            $string = preg_replace("/[^a-z0-9_\s-]/", "", $string);
            //Clean up multiple dashes or whitespaces
            $string = preg_replace("/[\s-]+/", " ", $string);
            //Convert whitespaces and underscore to dash
            $string = preg_replace("/[\s_]/", "-", $string);
            return $string;
        }
    }
    
    /* End of file blog.php */
    /* Location: ./application/controllers/blog.php */
    
    public function isInDb($table, $where = array())
    {
        $q = $this->db->get_where($table, $where);
        return ($q->num_rows() > 0) ? true : false; //returns either true or false, pretty straight forward
    }
    
    public function getColumn(){}