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

时间:2015-12-08 01:54:46

标签: php .htaccess yii yii2

我目前正在更新现有的实时网站以使用漂亮的网址。我已经有了网址,但我的问题是,在我启用了漂亮的网址后,Google等网站上的所有旧链接都将停止工作。他们只是重定向到首页。例子; 旧网址:http://www.dreambulgarianproperties.com/index.php?r=properties%2Fproperty%2Fview&id=37重定向到首页

新网址:http://www.dreambulgarianproperties.com/properties/property/view?id=37,因为它应该从两个网址中看到。

我的.htaccess文件是这样的:

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

我的UrlManager配置就是这个;

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

该网站运行正常,我只是担心如果我更改网址会丢失Google流量。

有没有办法告诉Yii继续解析旧网址,同时显示和处理新的漂亮网址?

2 个答案:

答案 0 :(得分:1)

我找到了整齐地做这件事。我扩展了UrlManager并更新了像这样的parseRequest()方法;

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, []];
}

现在如果有一个像index.php这样的查询字符串?r = controller / action它将处理它,否则它会将控制权传递给父UrlManager进行正常处理。

为了让我的搜索引擎更新,我在每个页面的标题中添加了一个规范链接,以便Google知道使用漂亮的网址进行索引。

答案 1 :(得分:-1)

/vendor/yiisoft/yii2/web/urlManager.php 内部将 parseRequest方法替换为此:

    public function parseRequest($request)
{   
    $enable = true;
    $url = Yii::$app->request->url;
    if ($pos = strpos($url, 'index.php?r=') == true) {
           $enable = false;
    }
    if ($enable) {
        /* @var $rule UrlRule */
        foreach ($this->rules as $rule) {
            $result = $rule->parseRequest($this, $request);
            if (YII_DEBUG) {
                Yii::trace([
                    'rule' => method_exists($rule, '__toString') ? $rule->__toString() : get_class($rule),
                    'match' => $result !== false,
                    'parent' => null,
                ], __METHOD__);
            }
            if ($result !== false) {
                return $result;
            }
        }

        if ($this->enableStrictParsing) {
            return false;
        }

        Yii::trace('No matching URL rules. Using default URL parsing logic.', __METHOD__);

        $suffix = (string) $this->suffix;
        $pathInfo = $request->getPathInfo();
        $normalized = false;
        if ($this->normalizer !== false) {
            $pathInfo = $this->normalizer->normalizePathInfo($pathInfo, $suffix, $normalized);
        }
        if ($suffix !== '' && $pathInfo !== '') {
            $n = strlen($this->suffix);
            if (substr_compare($pathInfo, $this->suffix, -$n, $n) === 0) {
                $pathInfo = substr($pathInfo, 0, -$n);
                if ($pathInfo === '') {
                    // suffix alone is not allowed
                    return false;
                }
            } else {
                // suffix doesn't match
                return false;
            }
        }

        if ($normalized) {
            // pathInfo was changed by normalizer - we need also normalize route
            return $this->normalizer->normalizeRoute([$pathInfo, []]);
        }

        return [$pathInfo, []];
    }

    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, []];
}

方法基本相同,区别在于前6行之内。