1

I have site based on Yii2 advanced template, made for replacing old site that cannot be optimized for SEO.

SEO-friendly urls are enabled in Yii2's urlManager configuration.

For SEO purposes I should write near 20 301 redirects from old site for urls with raw query string with structures:

/index.php?p=var
/index.php?p=open_cat&cat_id=55

to SEO-friendly urls ('contoller','action','alias' are variables):

/controller/action/alias

I have tried:

  1. In frontend/web/.htaccess:

    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
    

This method didn't work, page with /index.php?p=open_cat&cat_id=55 url responds 404 error.

  1. In frontend/web/.htaccess:

    RewriteEngine on
    
    RewriteCond %{QUERY_STRING} p=open_cat&cat_id=55
    RewriteRule ^index.php$ /controller/action/alias [L,R=301]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    
    RewriteRule . index.php
    

This method appends full path of index.php with raw route:

http://example.com/path-to-file-from-server-root/app/frontend/web/index.php?r=controller/action/alias

I don't know how to write correct redirect with Yii2 methods for raw url. Also problem is that old and new urls have nothing in common. I search unified solution for all redirects, because new urls are associated with different actions and controllers.

I would be glad for hint or solution. Thank you!

Daria Motorina
  • 60
  • 1
  • 2
  • 26
  • May be it helpful. [http://www.yiiframework.com/wiki/799/yii2-app-advanced-on-single-domain-apache-nginx/](http://www.yiiframework.com/wiki/799/yii2-app-advanced-on-single-domain-apache-nginx/) And [http://www.yiiframework.com/wiki/755/how-to-hide-frontend-web-in-url-addresses-on-apache/](http://www.yiiframework.com/wiki/755/how-to-hide-frontend-web-in-url-addresses-on-apache/) – ThanhPV Jul 17 '16 at 03:03
  • Thank you, but problem was from another side - pretty URL's are already configured on my site, I could not write correct rule in htaccess. Seems that solution is in redirect via headers in php code. – Daria Motorina Jul 17 '16 at 17:44

1 Answers1

0

Solution was taken from answer on this question.

I've created component RedirectComponent, included in frontend\config\main.php in components and bootstrap arrays:

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

I've created method init() in RedirectComponent class (not sure in necessity on parent::init() in it):

public function init()
{
      /*redirect logic*/ 
}

I couldn't redirect to proper location via redirect() method, called through dynamically created controller and action, so I've redirected via native php header() function.

Community
  • 1
  • 1
Daria Motorina
  • 60
  • 1
  • 2
  • 26