codeigniter - 使用 CodeIgniter 重写 url

标签 codeigniter seo codeigniter-routing

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

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

最佳答案

您好,您要的是一大堆代码,而您自己没有做任何研究。

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

  1. 相同的标题可以多次出现(产生可以避免的问题)
  2. 由于通过 slug 而不是 ID 查询(性能低下)导致加载/搜索缓慢
  3. 用户需要记住整个标题(id+title;用户可以复制部分损坏的 url www.example.com/234593/this-title-is-)/基于意见

为了使我的建议生效,您需要:

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

//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

设置 Controller (application/controllers/blog.php)

<?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 */

就是这样,现在创建自己的模型,可以验证 ID、获取标题...

示例代码(模型):

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(){}

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

此解决方案对 SEO 友好。

关于codeigniter - 使用 CodeIgniter 重写 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27506657/

相关文章:

php - Javascript 确认删除 - 如果没有不提交表单?代码点火器

javascript - 在codeigniter中将数据从javascript传递到php文件

ruby-on-rails - 如何将 id 添加到 Friendly_id slug?

html - 如何从Google服务搜索结果中删除日期

php - codeigniter 路由任何或 num 不工作

javascript - 显示页面其他位置的 JavaScript 隐藏元素

php - 如何在 codeigniter 中开始一个新的网站项目?

php - 在 WordPress 帖子中指定自定义规范 URL

codeigniter - 更改 url 以获得更好的 SEO