【问题标题】:Rewrite the url using CodeIgniter使用 CodeIgniter 重写 url
【发布时间】:2014-12-16 14:14:30
【问题描述】:

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

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

【问题讨论】:

  • 对不起,我明天试试,我现在在做另一个项目,我会更新主题。感谢您的帮助

标签: codeigniter seo codeigniter-routing


【解决方案1】:

您好,您要求提供大量代码,但您自己没有进行任何研究。

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

  1. 同一个标题可以出现多次(产生可以避免的问题)
  2. 由于使用 slug 而不是 ID 查询导致加载/搜索缓慢(性能缓慢)
  3. 用户需要记住整个标题(带有 id+title;用户可以复制部分损坏的网址 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

设置控制器(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 标头重定向到 /1/title)或者您可以使用(生成)www.example.com/1/title 链接,但是如果您生成 url 像:/1/fake-title(标题是对 id 1 无效,它不会重定向到正确的)

此解决方案对 SEO 友好。

【讨论】:

  • 我已经尝试过您的解决方案并且它有效,但我想删除 url 中的“/front/post/”是否可能?再次感谢。
  • 它的工作原理就是你只需要生成这样的链接...example.com/id/titleexample.com/id(重定向到example.com/id/title),请再次阅读答案。
猜你喜欢
  • 2020-06-03
  • 2011-06-14
  • 1970-01-01
  • 2012-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多