[英]Yii2 old links not working after pretty urls enabled
我目前正在更新现有的实时网站,以使用漂亮的网址。 我已经启用了网址,但是我的问题是,启用了漂亮的网址后,来自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在显示和处理漂亮的新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中,将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行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.