简体   繁体   English

在yii 1 Web应用程序中使用SEO漂亮的URL在URL上查看文章标题而不是ID

[英]View article title on url instead of id in yii 1 web application for SEO pretty urls

i have articles in my yii web application, i want to view article title instead of id at the url, i have followed this link: http://www.yiiframework.com/forum/index.php/topic/43874-urlmanager-show-title-instead-of-id/ i changed createUrl function to be : 我在yii Web应用程序中有文章,我想查看文章标题而不是URL上的id,我已点击此链接: http : //www.yiiframework.com/forum/index.php/topic/43874-urlmanager- show-title-instead-of-id /我将createUrl函数更改为:

Yii::app()->createUrl('home/article',array('id'=>$article->id,'title' => $article->title); 

and add the following rules to url manager as below : 并将以下规则添加到url管理器,如下所示:

        '<action:\w+>'=>'home/<action>',
            '<action:\w+>/<id:\d+>'=>'home/<action>',
             '<controller:\w+>/<id:\d+>'=>'<controller>/view',
            '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
            '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
              'article/<id>/<title>'=>'home/article/view/'

article url now like : http://localhost/test/article?id=6 i want to be 文章网址现在像: http:// localhost / test / article?id = 6我想成为
http://localhost/test/article/article-title http:// localhost / test / article / article-title

thank you in advance . 先感谢您 。

I do something similar, but you don't want to avoid using a unique Id to find your article, because you can't depend on titles being unique or even not ever changing. 我做了类似的事情,但是您不想避免使用唯一的ID来查找您的文章,因为您不能依赖标题是唯一的,甚至永远都不会改变。
It's good practice to not use an autonumeric Id at least publicly in the URL, so you could something like this: 最好不要在网址中至少公开使用自动数字ID,因此您可以执行以下操作:
"article/<key:\\w{6}>/<title>"

So, the URLs will be like: article/e87qy2/The_book_title_here And you just ignore the title and use the key that must be unique for every article. 因此,URL将类似于: article/e87qy2/The_book_title_here并且您只需忽略标题,并使用对于每个文章都必须唯一的
Of course, an alphanumeric hash or key is not very diferent than a numeric Id, but at least you got rid of revealing interal information about your Database by showing that you have autonumeric primary keys. 当然,字母数字散列或键与数字ID并不完全不同,但是至少您可以通过显示具有自动数字主键来摆脱有关数据库的内部信息的麻烦。

If you really really unavoidably want to have just the title in the url, well, first make sure any newly created article is unique, and then just use it as a unique key to find the article. 如果您确实确实不可避免地希望仅在URL中添加标题,那么首先确保任何新创建的文章都是唯一的,然后将其用作查找文章的唯一关键字。
Avoiding two equal articles may require that you add numbers 2, 3, etc. at the end to make them different. 避免两篇相等的文章可能要求您在末尾添加数字2、3等,以使它们有所不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM