Answer the question
In order to leave comments, you need to log in
Yii2 how to make 301 redirect from normal url to cnc?
There is a site on Yii2 on an advanced template.
UPD. The site has already configured CNC for urls - in the .htaccess folder in the root of the site, in the .htaccess folder frontend / web and in the urlManager config, the rules for all the necessary routes are written.
There is a technical specification for SEO, in which it is required to make 301 from the urls of the old, independent of the current site of the form:
UPD. The list of redirects for SEO was provided after the full development of the site on Yii2, so there is no direct connection between the urls of the old and the new site.
/index.php?p=var
/index.php?p=open_cat&cat_id=55
/controller/action
/controller/action/alias
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Redirect 301 /index.php?p=open_cat&cat_id=55 http://example.com/controller/action/alias
RewriteRule . index.php
RewriteEngine on
RewriteCond %{QUERY_STRING} p=open_cat&cat_id=55
RewriteRule ^index.php$ /controller/action/alias [R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
http://example.com/controller/action/alias
http://example.com/path-to-file-from-server-root/app/frontend/web/index.php?r=controller/action/alias
Answer the question
In order to leave comments, you need to log in
Solution:
1. Create a component class, e.g. RedirectComponent, inherit it from \yii\base\Component
2. In frontend\config\main.php write this class in components and bootstrap, example:
<?php
$params = array_merge(
require(__DIR__ . '/../../common/config/params.php'),
require(__DIR__ . '/../../common/config/params-local.php'),
require(__DIR__ . '/params.php'),
require(__DIR__ . '/params-local.php')
);
return [
'id' => 'app-frontend',
'basePath' => dirname(__DIR__),
'homeUrl' => '/',
'bootstrap' => ['RedirectComponent','log'],
'controllerNamespace' => 'frontend\controllers',
'components' => [
/* */
'RedirectComponent' => [
'class'=>'frontend\components\RedirectComponent',
],
],
'params' => $params,
];
public function init()
{
/*redirect logic*/
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question