Php Yii2启用漂亮URL后旧链接不起作用

Php Yii2启用漂亮URL后旧链接不起作用,php,.htaccess,yii,yii2,Php,.htaccess,Yii,Yii2,我目前正在更新一个现有的实时网站,以使用漂亮的URL。我已经让URL正常工作了,但我的问题是,在我启用了漂亮的URL之后,来自Google等网站的所有旧链接都将停止工作。他们只是重定向到头版。实例; 旧url:重定向到首页 新url:从两个url都可以看到 我的.htaccess文件如下: RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(

我目前正在更新一个现有的实时网站,以使用漂亮的URL。我已经让URL正常工作了,但我的问题是,在我启用了漂亮的URL之后,来自Google等网站的所有旧链接都将停止工作。他们只是重定向到头版。实例; 旧url:重定向到首页

新url:从两个url都可以看到

我的.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>'
        ],
    ],
'urlManager'=>[
“enablePrettyUrl”=>true,
'showScriptName'=>false,
'enableStrictParsing'=>false,
“规则”=>[
''=>'站点/'
],
],
该网站运行良好,我只是担心如果我更改URL,会失去谷歌的流量


有没有一种方法可以告诉Yii继续解析旧的URL,同时显示和处理新的漂亮URL?

我找到了一种巧妙的方法。我扩展了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进行正常处理


为了使我的seo保持最新,我在每个页面的页眉中添加了一个规范链接,以便Google知道如何使用漂亮的url进行索引。

/vendor/yiisoft/yii2/web/urlManager.php中替换解析请求方法

    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行。

我不会说新URL比旧URL更漂亮。它们在关键词内容上没有区别,我想谷歌不会在意这一级别上的任何变化。但是,您需要添加从旧URL到新URL的永久重定向,否则对您的用户来说,这将是一个非常烦人的体验。@Sven我知道,稍后我将添加关键字内容。我希望避免为每个页面硬编码重定向,这将是一个正确的PITA。您可以设置一个自定义404错误处理程序来处理URI,并检查它是否与新结构中的url匹配,如果匹配,则执行301重定向。类似于:
if(Yii::app()->errorHandler->error->code==404)
@Tristan问题是Yii不会生成任何错误。它只是决定正在使用漂亮的URL,忽略index.php之后的任何内容(除了查询字符串),并尝试基于该内容生成页面。它最终总是显示主页,而不记录任何错误。@rob006在我看来,没有其他方法可以处理旧的url