dpgbh20688 2015-12-08 01:54
浏览 26
已采纳

在启用漂亮网址后,Yii2旧链接无法正常工作

I'm currently updating an existing live website to use pretty urls. I've got the urls working, but my problem is that after I've enabled pretty urls all of the old links from sites like Google etc will stop working. They simply redirect to the front page. Examples; Old url: http://www.dreambulgarianproperties.com/index.php?r=properties%2Fproperty%2Fview&id=37 Redirects to the front page

New url: http://www.dreambulgarianproperties.com/properties/property/view?id=37 as it should look from both urls.

My .htaccess file is this:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule  ^(.*)$ index.php/$1

and my UrlManager config is this;

'urlManager' => [
        'enablePrettyUrl' => true,
        'showScriptName' => false,
        'enableStrictParsing' => false,
        'rules' => [
            '<action:\w+>' => 'site/<action>'
        ],
    ],

The site works fine, I'm just worried about losing Google traffic if I change the urls.

Is there a way of telling Yii to continue parsing old urls, while displaying and handling the new, pretty urls?

  • 写回答

2条回答 默认 最新

  • dongra1984 2015-12-08 13:25
    关注

    I found away to do this neatly. I extended the UrlManager and updated the parseRequest() method like this;

    public function parseRequest($request) {
        $url = Yii::$app->request->url;
        if ($pos = strpos($url, 'index.php?r=') == false) {
            return parent::parseRequest($request);
        }
        Yii::trace('Pretty URL not enabled. Using default URL parsing logic.', __METHOD__);
        $route = $request->getQueryParam($this->routeParam, '');
        if (is_array($route)) {
            $route = '';
        }
    
        return [(string) $route, []];
    }
    

    Now if there is a query string like index.php?r=controller/action it will process it, otherwise it passes control back to the parent UrlManager for normal processing.

    To keep my seo up to date I've added a canonical link in the header of every page so Google knows to use the pretty url for indexing.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测