使用Codeigniter的PHP路由(SEO中的标题) [英] PHP routing with Codeigniter (Titles in URL for SEO)

查看:148
本文介绍了使用Codeigniter的PHP路由(SEO中的标题)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些问题与Codeigniter协调路由。我现在做的是以下:

  $ route ['articles /(:num)'] =' view / $ 1'; // $ 1将包含ID 

这意味着example.com/articles/123将正常工作,加载一个ID为123的文章。但是我也希望有可能将aticle的标题添加到URL(用于SEO)。示例:example.com/articles/123/article-title



我想要的是和Stack Overflow几乎一样的东西:stackoverflow.com/questions/123/the



我还想知道Stack Overflow是如何工作的。如果我去stackoverflow / questions / 111标题会自动添加到url。这是用php重定向()?

解决方案

我做了类似的过去;我不能找到它知道但是IIRC(它是几个月前)你可以使用像你做的路线,并添加一个更具体的一个,如

  $ route ['articles /(:num)/(:any)'] ='articles / view / $ 1 / $ 2'; 
$ route ['articles /(:num)'] ='articles / view / $ 1';

正如你可以看到,都映射到同一个方法,这是一种重载您可以使用默认值补偿缺少的参数:

 函数文章($ id,$ slug = FALSE)
{}

并且简单地忽略文章检索中的第二个参数。



对于添加标题,您可以:


  1. 在数据库中有一个在保存文章时创建。您可以使用 url_title($ title,'dash',TRUE)函数(在url帮助中) code>使用 dash 作为分隔符,并将其全部小写;

  2. 使用上述函数并转换标题的文章(在从数据库中检索它之后)on-the-fly;只要检查article()方法,如果第二个参数不是false,你会知道你是否需要创建slug;

至于如何显示lug,即使使用没有它的url,你可以使你,如你所猜测的重定向,但由于两个路由指向同一个方法,它不会改变任何为你。



Oh,uhm,在调用重定向时要小心,请仔细检查;)


I have some questions concering routing with Codeigniter. What I´m doing now is the following:

$route['articles/(:num)'] = 'articles/view/$1'; // $1 will contain an ID

This means that example.com/articles/123 will work perfectly and load an article with an ID of 123. But I also want to have the possiblilty to add the aticle´s title to the URL (for SEO). Example: example.com/articles/123/article-title

What I want is pretty much the same thing as Stack Overflow: stackoverflow.com/questions/123/the-title

How can I do that?

I´m also wondering how Stack Overflow works. If I go to stackoverflow/questions/111 the title will automatically be added to the url. Is that done with php redirect()?

解决方案

I have done something similar in the past; I can't find it know but IIRC (it was months ago) You can use a route like you did, and also add a more specific one, like

$route['articles/(:num)/(:any)'] = 'articles/view/$1/$2';
$route['articles/(:num)'] = 'articles/view/$1';

As you can see, both map to the same method, which is kind of "overloaded"; you can make up for a missing parameter by using a default value:

function articles($id,$slug = FALSE) 
{ }

and simply ignore the second parameter in your article retrieval.

As for adding the title you can:

  1. have a "slug" field in your database, created when the article is saved. You can use the comfortable url_title($title,'dash',TRUE) function (in the url helper), which takes the $title, uses the dash as separator, and make it all lowercase;
  2. use the above function and convert the title of the article (after you retrieved it from the database) "on-the-fly"; just check on the article() method if the 2nd parameter isn't false and you'll know if you need to create the slug or not;

As for how to show the slug even when using an url without it you can make, as you guessed, a redirect, but since both routes point to the same method it won't change anything for you.

Oh, uhm, beware of loops while calling the redirect, check carefully ;)

这篇关于使用Codeigniter的PHP路由(SEO中的标题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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