搜索引擎优化Yii框架友好的URL与ID和名称框架、友好、搜索引擎优化、名称

由网友(故人叹)分享简介:我一直在寻找各地的#1和YII论坛上,有很多答案,这并没有帮助我...... I've been searching all around Stackoverflow and YII forums, there are many answers,which didn't help me...这是我的情况。 我...

我一直在寻找各地的#1和YII论坛上,有很多答案, 这并没有帮助我......

I've been searching all around Stackoverflow and YII forums, there are many answers, which didn't help me...

这是我的情况。

我有控制器名为:凸出并称为操作查看。 它得到: ID (INT),名称(串)

I have controller called: proj and an action called view. It gets: id(int), name(string).

所需的名称有时会用特殊字符如: +,#$%^&放大器; * - !]

The desired name gets sometimes with special chars such as: [+,!#$%^&*-]

所以,当我跑的 createUrl()函数返回我不那么友好的URL。

So when I'm running createUrl() function it returns me not so friendly url.

例如: http://www.qa-mysite.com/proj/1029/Conservation+of+the+Vermont+Salt+Pan+System%2C+Hermanus%2C+South+Africa.

ID = 1029 名称=养护佛蒙特州盐田系统,赫曼努斯,南非。

id = 1029 name = Conservation of the Vermont SaltPan System, Hermanus, South Africa.

我希望得到的结果是: http://www.qa-mysite.com/proj/1029/conservation-of-the-vermont-salt-pan-system-hermanus-south-Africa

I want the result to be: http://www.qa-mysite.com/proj/1029/conservation-of-the-vermont-salt-pan-system-hermanus-south-Africa

所以,其实我需要剥离的特殊字符,改变分隔符空格之间 - 代替+

So actually i need to strip the special chars and change the delimiter between spaces to "-" instead of "+".

我的电流通过CUrlManager的配置:

My current configurations of the curlManager are:

'urlManager'=>array(
            'urlFormat'=>'path',
            'showScriptName'=>false,
            'appendParams' => true,
            'rules'=>array(
            //array('proj/view/<name:w+>', 'pattern'=>'proj/<id:d+>'),
                //'<controller:w+>/<id:d+>'=>'<controller>/view',
                'proj/<id:d+>/<name:w+>'=>array('proj/view', 'caseSensitive'=>false),
                '<controller>/<id:d+>/<name:.*?>'=>'<controller>/view',
                '<controller:w+>/<action:w+>/<id:d+>'=>'<controller>/<action>',
                '<controller:w+>/<action:w+>'=>'<controller>/<action>',
            ),
        )

CreateUrl例如:

CreateUrl example:

$this->createUrl('proj/view', array('id' => $data->id, 'name' => $data->name));

在该网址将被改变,我需要做的 301重定向到新的,搜索引擎友好的URL的旧

After the urls will be changed I need to do 301 redirects of the old ones to the new-seo-friendly urls.

注:我不能做硬codeD str_replace转换

Note: I cannot do hardcoded str_replace.

非常感谢您的帮助:]

Many thanks for any help :]

推荐答案

通过继承创建您自己的URL管理通过CUrlManager 和ovveride createUrl 有点,例如:

Create your own url manager by subclassing CUrlManager and ovveride createUrl a bit, in example:

class MyUrlManager extends CUrlManager {
    public function createUrl($route,$params=array(),$ampersand='&') {
        if($route == 'proj/view' && isset($params['name'])) {
            $params['name'] = processYourParamFunction($params['name']);
        }
        return parent::createUrl($route,$params,$ampersand);
    }
}

然后修改config来使用这个类:

Then modify your config to use this class:

...
'urlManager' => [
    'class' => 'MyUrlManager'
...
]

这是一个伟大的Yii的特征之一,控制自然反转:)

有关问题的第二部分:

在您查看的行动只是重定向到新的URL与createurl如果检测不想要的字符。只要确保你不打重定向循环。

In you view action simply redirect to new url with createurl if you detect unwanted characters . Just make sure you not hit redirect loop.

更新:

要重定向与301只通过重定向codeA第三个参数来重定向呼叫:

To redirect with 301 just pass redirect code a third param to redirect call:

$this->redirect('route', true, 301);

边注:

使用 cannonical 指向搜索引擎,以正确的URL

Use cannonical to point search engines to proper url

阅读全文

相关推荐

最新文章