D
D
Daria Motorina2016-07-17 00:54:58
Yii
Daria Motorina, 2016-07-17 00:54:58

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

On the CNC, the urls of the current site of the form:
/controller/action
/controller/action/alias

My attempts to solve this problem through .htaccess in the app/frontend/web folder:
1. Make a normal redirect - it doesn't work, the page gives a 404 error:
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

2. Make a redirect via RewriteCond and RewriteRule:
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

It turns out instead of the expected url:
http://example.com/controller/action/alias
such a url that also does not work:
http://example.com/path-to-file-from-server-root/app/frontend/web/index.php?r=controller/action/alias

Perhaps there is an option to solve this using Yii2 in controllers, but I do not understand how to direct such a url to an action.
The redirected actions are located in different controllers, I would like to find a common solution for them.
I would be grateful for any hint towards a solution. Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Daria Motorina, 2016-07-17
@glaphire

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

3. In the RedirectComponent class, write a method with redirects:
public function init()
{
      /*redirect logic*/ 
}

I failed to create a redirect through a dynamic controller and an action, I used a redirect through header().

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question