htaccess中重定向的规则 [英] Rules to redirect in htaccess

查看:158
本文介绍了htaccess中重定向的规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have old urls to my news like http://example.com/mynew/35/this-is-the-title, where 35 is the Id of the news. Now my new urls is like http://example.com/mynew/this-is-the-title I have no idea how get a redirect to all old news url to new urls. Any idea? Thanks

在我的route.php中的codeigniter中,我有:

In my routes.php in codeigniter I have:

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


推荐答案

在这种情况下, http ://example.com/mynew/35/this-is-the-title 您已经有了表格的ID,就可以轻松找到它。但是,如果您使用此 http://example.com/mynew/this-is-the-title ,则没有任何唯一标识符,因此您将无法从表。

In this case, http://example.com/mynew/35/this-is-the-title you've id of the table and you can easily find it through. But if you use this http://example.com/mynew/this-is-the-title there is no any unique identifier so you can't retrieve data from the table.

要对此进行存档,您可以执行以下操作

To archive this, you can do these


  1. 更改带有URL链接的表(列名 url_slug slug

  2. 创建通用控制器方法。根据您的代码 index()新闻(控制器名称)下

  3. 创建公共新闻视图

  1. Alter table with URL slug( column nameurl_slug or slug)
  2. create common controller method. As per your code index() under news(controller name)
  3. create common news view








注意: 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>

这篇关于htaccess中重定向的规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆