【问题标题】:Rules to redirect in htaccess在 htaccess 中重定向的规则
【发布时间】:2018-09-11 07:51:14
【问题描述】:

我的新闻有旧网址,例如 http://example.com/mynew/35/this-is-the-title,其中 35 是新闻的 ID。现在我的新网址就像http://example.com/mynew/this-is-the-title 我不知道如何将所有旧新闻网址重定向到新网址。任何想法?谢谢

在 codeigniter 的 routes.php 中,我有:

$route['news'] = 'news/index';
$route['news/(:num)'] = 'news/index/$1';
$route['mynew/(:any)'] = 'news/mynew/$1';

【问题讨论】:

  • 我猜this-is-the-titlemynew index() 函数的参数?在这种情况下,只需测试第一个参数,例如35 是一个整数,如果是,则获取第二个参数并将其用于您的查询。
  • 此外,您是要实际重定向还是仅使用相同的 url 但以不同的方式路由?因为您标记了 htaccess 并提供了路由,所以我不确定您想要实现哪个。

标签: php .htaccess codeigniter


【解决方案1】:

在这种情况下,http://example.com/mynew/35/this-is-the-title 您已经拥有该表的 id,并且您可以轻松找到它。但是,如果您使用这个http://example.com/mynew/this-is-the-title,则没有任何唯一标识符,因此您无法从表中检索数据。

要存档,您可以这样做

  1. 使用 URL slug 更改表(列名url_slugslug
  2. 创建通用控制器方法。根据news(控制器名称)下的代码index()
  3. 创建通用新闻视图

注意:Slug 应包含唯一的文本标识符以识别确切的新闻(与主键相同)

Title - this is an news title regarding php
slug - regarding-php

在控制器中

public function index($slug)
{
    if (!empty($slug)) {
        # code...
        #write select code and load the common view
        $data['title'] = "my Heading" ;
        $data['content'] = "my para............." ;
        $data['footer'] = "foot" ;

        $this->load->view('news/common',$data);
    }
    else
    {

    }
}

可见

 # echo will also work

<div id="title"><?= print_r($title); ?></div>
<div id="content"><?= print_r($content); ?></div>
<div id="footer"><?= print_r($footer); ?></div>

【讨论】:

  • 我认为这根本不是用户要问的,我相信他们已经有了这种类型的系统,只是想修复路由以支持 url 结构所在的旧 url略有不同。
  • 路由通过id调用时无法使用。除非他将所有 id 硬编码为路由
  • 是的。我已经解决了控制器和视图。我需要的是在谷歌中重定向 301 特别思考。所以当clic在example.com/mynew/35/this-is-the-title重定向到example.com/mynew/this-is-the-title
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-24
  • 2017-08-29
  • 2012-12-24
  • 2010-12-22
  • 2011-01-05
相关资源
最近更新 更多