menxaca
(Menxaca)
1
Hi All,
I’m not a SEO expert, I have read that having domain.com/post/title-post-testing-1 is better than having domain.com/post/title+post+testing-1
First of all… is that true? or it really doesn’t matter?
And how to achieve that? Because I always get the title like this title+post+testing-1, I have tried several options but any of them work.
URL Manager
...
'post/<title>-<id:\d+>'=>'blog/post/view'
...
I have read this wiki used for action controllers, do I have to do something similar?
Thanks in advance!
\d+ is for digits only. Use somehting like [\w\-]+ instead.
menxaca
(Menxaca)
3
Thanks once again ORey for your response in one of my topic.
I have tried your tip but it doesn’t work as expected:
...
'post/<title:[\w\-]+>-<id:\d+>'=>'blog/post/view',
...
I have played with it, trying other options, but I always get the title separated with + sign
Any other tip is more than welcome, where I could search to find example rules or something like this?
Thanks!
ok, there are two parts actually:
-
creating the URL
-
parsing the URL.
Second part is already covered by your url rule. Now all urls that satisfy this regexp will be passed to blog/post/view.
Now the first part. Show me how you create your urls.
Speaking about the plus sign, it’s the usual behaviour of url_encode() used by UrlManager.
You can override this in child classes.
Either investigate the code by yourself (it can be easily found), or wait till tomorrow, I can find this place for you.
I mean, if you do something like this
createUrl('/blog/post/view', array('title' => 'Nice title with spaces'))
the result will be something like
/blog/post/view/title/Nice+title+with+spaces
It can be changed by many ways, including:
-
creating a slug for every post (kind of ID but text, ‘nice-title-with-spaces’)
-
overriding default urlmanager
-
…
and so on.
I’m not an expert also, but my SEO guys always want text instead of IDs. This freaks me out ![:)](http://www.yiiframework.com/forum/public/style_emoticons/default/smile.gif)
menxaca
(Menxaca)
8
I have a method on post model:
public function getUrl()
{
return Yii::app()->createUrl('blog/post/view', array(
'id'=>$this->id,
'title'=>$this->title,
));
}
So I always call to this function, the URL result looks like this right now: ‘http://domain.com/post/Testing+Post+Title-1’
menxaca
(Menxaca)
9
Thanks ORey, I will try some other things, but if you find some other hint, please let me know, so much appreciate.
IMHO, the simplest solution is to change
'title'=>$this->title,
to something like
'title'=>some_function($this->title),
or, better, create a getter in your model, like
public function getSlug()
{
return some_function($this->title);
}
then you will be able to use
return Yii::app()->createUrl('blog/post/view', array(
'id'=>$this->id,
'title'=>$this->slug,
));
some_function() should change all the spaces and other unwanted characters to ‘-’.
menxaca
(Menxaca)
11
I was thinking in something similar, I have created a getter and used str_replace, works like a charm! thanks once again ORey!