Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added SEORedirectBehavior to redirect old links to new ones #20

Merged
merged 2 commits into from
Mar 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/config/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,54 @@
],
'response' => [
'class' => \yii\web\Response::class,
'as urlRedirect' => [
'class' => \hipanel\site\helpers\SEORedirectBehavior::class,
'redirectMap' => [
'news/\d+' => 'site/index',

'cart' => 'cart/cart/index',

'login' => 'site/login',
'login/remind' => 'site/login',

'registration' => 'site/login',
'domains' => 'site/index#tld-table',
'transfer' => 'domain/transfer/index',
'search/whois' => 'domain/whois/index',
'search/bulk' => 'domain/check/check-domain',

'dns' => 'site/dns',

'vds' => 'site/vds',
'vds/price' => 'site/vds',

'help' => 'faq/faq/index',

'rules' => 'pages/rules/index#termsOfUse',
'pages/contact' => 'site/contact',
'resellers' => 'pages/api/index',
'resellers/intercept' => 'pages/api/intercept',

'rules/cancelation_policy' => 'pages/rules/index#cancelationPolicy',
'rules/deletion_policy' => 'pages/rules/index#domainRemovalAndAutoRenewalPolicy',
'rules/privacy' => 'pages/rules/index#privacyPolicy',
'domain_registration' => 'pages/rules/index#domainNameRegistrationAgreement',

'pages/about' => 'site/about',
'rules/usage' => 'pages/rules/index#vps_terms_of_use',

// not found
'domains/com' => 'site/index',
'promotion' => 'site/index',
'pages/loyalty' => 'site/index',
'news' => 'site/index',
'vds/pages/monitoring' => 'site/vds',
'vds/pages/support' => 'site/vds',
'vds/pages/equipment' => 'site/vds',
'vds/pages/solutions' => 'site/vds',
'domains/zones/april' => 'site/index',
]
]
],
'urlManager' => [
'class' => \codemix\localeurls\UrlManager::class,
Expand Down
44 changes: 44 additions & 0 deletions src/helpers/SEORedirectBehavior.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace hipanel\site\helpers;

use Yii;
use yii\base\Behavior;
use yii\web\Response;
use yii\web\UrlRule;

class SEORedirectBehavior extends Behavior
{
/**
* @var array
*/
public $redirectMap = [];

/**
* @var int
*/
public $statusCode = 302;

public function events()
{
return [
Response::EVENT_BEFORE_SEND => 'beforeSend'
];
}

public function beforeSend()
{
if (Yii::$app->response->statusCode === 404) {
$pathInfo = Yii::$app->request->pathInfo;
if (!empty($pathInfo)) {
foreach ($this->redirectMap as $pattern => $redirect) {
$rule = (new UrlRule(['pattern' => $pattern, 'route' => $pathInfo]));
if (preg_match($rule->pattern, $pathInfo, $matches)) {
Yii::$app->response->redirect('/' . $this->redirectMap[$rule->name], $this->statusCode)->send();
Yii::$app->end();
}
}
}
}
}
}