E
E
Evgeny Sheleg2013-09-12 14:17:18
Yii
Evgeny Sheleg, 2013-09-12 14:17:18

Yii + Nginx "rewrite in rewrite"

Hello.

Please tell me how to write this rule correctly:

location ~ ^/(aaa|bbb|ccc) { try_files $uri $uri/ /$yii_bootstrap?$args; } location ~ ^/([a-zA-Z0-9_-]+$) { rewrite ^/([a-zA-Z0-9_-]+$) /bbb/?str=$1 last; }

Meaning of the code: If we request a link with aaa, bbb or ccc controller, then we get to the controller. If another arbitrary link is requested, then we get to the controller with parameter substitution.
Everything works if sent to a regular link /bbb.php?str=, but under this condition the variable goes as a controller and works:
try_files $uri $uri/ /$yii_bootstrap?$args;

Thanks for any hint.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
Tehnomag, 2013-09-12
@Tehnomag

Via yii route means
in config/yii

'components'=>array(
...
'urlManager'=>array(
  'urlFormat'=>'path',
  'showScriptName'=>false,
  'rules'=>array(
                '/'=>'',
                array('/aaa/default/index/id/58058', 'caseSensitive'=>false, 'pattern' => 'aaa', 'verb' => 'GET', 'parsingOnly' => true),
                array('/bbb/default/index', 'caseSensitive'=>false,  'pattern' => 'bbb.php', 'verb' => 'GET', 'parsingOnly' => true),
                array('/bbb/default/index', 'caseSensitive'=>false,  'pattern' => 'bbb', 'verb' => 'GET', 'parsingOnly' => true),
                array('/ccc/default/index', 'caseSensitive'=>false,  'pattern' => 'ccc', 'verb' => 'GET', 'parsingOnly' => true),
  ),
 ),
),

then on mysite.ru/aaa -> will call module aaa, controller default, action index.
hang on 404 handler
 'components'=>array(
...
'errorHandler' => array(
   // use 'site/error' action to display errors
   'errorAction' => 'site/error',
  ),
),

will call the site control, the error method
there, we already check what to display depending on the error code
public function actionError()
 {
  $error=Yii::app()->errorHandler->error;
  if($error) {
   
  if(Yii::app()->request->isAjaxRequest){ 
   echo $error['message'];  
   Yii::app()->end(); 
  
  } else {

   if(!empty($error['code']) && $error['code'] == 404 && !empty($error['traces'][0]['args'][0]))
   {
    echo 'Query : '.$error['traces'][0]['args'][0];
    $this->redirect(array('bbb/index','str'=>$error['traces'][0]['args'][0]));
   }
  echo $error['message'];
 }

 }

in nginx:
location / {
        try_files $uri $uri/ /index.php?$args;
  root /var/www/site;
  index index.php index.html index.htm;
 }

 location ~ \.php$ {
   
  try_files $fastcgi_script_name =404;
  include /etc/nginx/fastcgi_params;
  fastcgi_pass   PollFPM;
  fastcgi_index index.php;
  fastcgi_param  SCRIPT_FILENAME  /var/www/site$fastcgi_script_name;
 }

F
fredrsf, 2015-01-17
@fredrsf

Please tell me why this line? try_files $fastcgi_script_name =404; On my project, for all non-existing .php files, it gives 404 using nginx, is it possible to make 404 be given using the framework?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question