htaccess中重定向的规则 [英] Rules to redirect in 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
- 更改带有URL链接的表(列名
url_slug
或slug
) - 创建通用控制器方法。根据您的代码
index()
在新闻
(控制器名称)下 - 创建公共新闻视图
- Alter table with URL slug( column name
url_slug
orslug
) - create common controller method. As per your code
index()
undernews
(controller name) - 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屋!