【问题标题】:Custom Urls in Codeigniter domain.com/articlename.htmlCodeigniter domain.com/articlename.html 中的自定义 URL
【发布时间】:2014-03-05 13:07:59
【问题描述】:

我需要在 Codeiginter 中创建自定义 URL 以显示来自的链接

domain.com/news/id/1 到 domain.com/article-name.html

我希望它对所有新闻 ID 都具有动态性,而不仅仅是一个链接

我可以在codeiginter中做吗?

【问题讨论】:

  • 是的,您目前是在这个项目中使用 CodeIgniter 还是只是在做研究?
  • 我已经完成了我的项目,但我害怕丢失了我在谷歌中有很好的 serp 的旧网址

标签: php codeigniter mod-rewrite friendly-url


【解决方案1】:

在您的 config.php 文件中执行以下操作:

$config['url_suffix'] = '.html'; // this line might actually not be necessary, i forget

routes.php 中设置:

$route['article-name.html'] = "news/id/1";

如果您遇到任何错误,请告诉我。

更新

第 1 步:在 routes.php 中设置默认控制器

$route['default_controller'] = 'get_article';

第 2 步:设置 config.php 以将 .html 放在末尾​​strong>

$config['url_suffix'] = '.html';

第 3 步:创建控制器

// path:   /codeigniter/2.1.4/yourAppName/controllers/get_article.php

<?php
class Get_article extends CI_Controller {

    public function index($article = '')
    {
        // $article will be "article-name.html" or "article2-name.html"
        if($article != '')
        {
            $pieces = explode('.', $article);

            // $pieces[0] will be "article-name"
            // $pieces[1] will be "html"

            // Database call to find article name
            // you do this

            $this->load->view('yourArticleView', $dbDataArray);
        }
        else
        {
            // show a default article or something
        }
    }
}
?>

【讨论】:

  • monkeyzeus 它不适用于每个 id 我希望它为每个文章 id 动态
  • 您希望domain.com/news/id/2 成为什么?
  • domain.com/new/id/2 将是来自数据库的新闻 2 的标题, domain.com/new/id/3 将是 domain.com/article3-title-from-database
【解决方案2】:

是的,您可以,这称为 URL 重写。我可以在这里解释,但官方文档要好得多。

这里是:http://ellislab.com/codeigniter/user-guide/general/urls.html

【讨论】:

    猜你喜欢
    • 2014-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-13
    • 1970-01-01
    • 1970-01-01
    • 2016-10-25
    • 1970-01-01
    相关资源
    最近更新 更多