<?php

$url=$_SERVER['REQUEST_URI'];
if(strpos($url,'?')!==false)
	$url=substr($url,0,strpos($url,'?'));

//if($_SERVER['REMOTE_ADDR']=='176.123.49.39'){
	if($url=='/index.php'){
		header("Request-URI: https://riavrn.ru/");
		header("Content-Location: https://riavrn.ru/");
		header("Location: https://riavrn.ru/");
		die();
	}
//}
	$photo=array();
	if(preg_match('/^\/photo\/\w+?\/(\d+?)\/.*$/',$url,$photo)){
		$db = new mysqli('localhost','riavrn','uOhKF6JX','riavrn.ru');
		if($code=$db->query("SELECT item_slug FROM articles_galleries,articles_items WHERE gallery_id='".intval($photo[1])."' AND article_id=id_item LIMIT 1")->fetch_assoc()) {
			header("Request-URI: https://riavrn.ru/news/" . $code['item_slug'] . '/');
			header("Content-Location: https://riavrn.ru/news/" . $code['item_slug'] . '/');
			header("Location: https://riavrn.ru/news/" . $code['item_slug'] . '/');
			die();
		}

	}
//if($_SERVER['REMOTE_ADDR']=='176.123.49.39'){
	$tv=array();
	if(preg_match('/^\/tv\/(\d+?)\/.*$/',$url,$tv)){
		$db = new mysqli('localhost','riavrn','uOhKF6JX','riavrn.ru');
		if($code=$db->query("SELECT item_slug FROM articles_videos,articles_items WHERE video_id='".intval($tv[1])."' AND article_id=id_item LIMIT 1")->fetch_assoc()) {
			header("Request-URI: https://riavrn.ru/news/" . $code['item_slug'] . '/');
			header("Content-Location: https://riavrn.ru/news/" . $code['item_slug'] . '/');
			header("Location: https://riavrn.ru/news/" . $code['item_slug'] . '/');
			die();
		}

	}
//}
//}
if(preg_match('/^\/news\/\d{4}\/\d{2}\/.*$/',$url)){
	header("Request-URI: https://riavrn.ru/news/");
	header("Content-Location: https://riavrn.ru/news/");
	header("Location: https://riavrn.ru/news/");
	die();
}

	if(preg_match('/^\/districts\/(\w+?)\/\d{4}\/\d{2}\/.*$/',$url,$f)){
		header("Request-URI: https://riavrn.ru/districts/".$f[1]."/");
		header("Content-Location: https://riavrn.ru/districts/".$f[1]."/");
		header("Location: https://riavrn.ru/districts/".$f[1]."/");
		die();
	}


//	var_dump($url);var_dump(preg_match('/^\/article\/(\w+?)\/.*$/',$url,$f));var_dump($f);die();
	if(preg_match('/^(\/[\w\d\-]+?)?\/article\/([\w\d\-]+?)\/.*$/',$url,$f)){
		header("Request-URI: https://riavrn.ru/news/".$f[2]."/");
		header("Content-Location: https://riavrn.ru/news/".$f[2]."/");
		header("Location: https://riavrn.ru/news/".$f[2]."/");
		die();
	}


	//https://riavrn.ru/gov/kak-vernut-podokhodnyy-nalog-s-zarplaty-vozvrat-13-protsentov-ndfl/
	if(preg_match('/^\/([\w\d\-]+?)\/trend\/([\w\d\-]+?)\/.*$/',$url,$f)){
		header("Request-URI: https://riavrn.ru/".$f[1]."/".$f[2]."/");
		header("Content-Location: https://riavrn.ru/".$f[1]."/".$f[2]."/");
		header("Location: https://riavrn.ru/".$f[1]."/".$f[2]."/");
		die();
	}




//	die();
//}
if(!in_array($_SERVER['HTTP_HOST'],array('riavrn.ru'))){

	if($_SERVER['REMOTE_ADDR']=='176.123.49.5'){
//		var_dump($url);die();
	}

	if(
			$url!='/'&&
			$url!='/news/'&&
			$url!='/news'&&
			!in_array($url,array('/accidents','/accidents/','/gov','/gov/','/economy','/economy/','/sport','/sport/','/society','/society/','/culture','/culture/','/health','/health/'))&&
			!preg_match('/^\/news\/([\w\d\-]+?)(\/)?$/',$url)&&
			!preg_match('/^\/page\/([\d]+?)(\/)?$/',$url)
	){
//			var_dump(preg_match('/^\/news\/([\w\d\-]+?)(\/)?$/',$url));var_dump($url);die();
		header("Request-URI: https://riavrn.ru".$_SERVER['REQUEST_URI']);
		header("Content-Location: https://riavrn.ru".$_SERVER['REQUEST_URI']);
		header("Location: https://riavrn.ru".$_SERVER['REQUEST_URI']);
		die();
	}
//		var_dump($url);
}
//if($_SERVER['REMOTE_ADDR']=='176.123.49.39'){
//	die();
	if(key_exists('page',$_GET)&&preg_match('/^\d+?$/',$_GET['page'])===0){
		unset($_GET['page']);
	}

//}
//	var_dump($_SERVER['HTTP_HOST']);die();
//}
//header("Access-Control-Allow-Origin: *");

defined('YII_DEBUG') or define('YII_DEBUG', false);
defined('YII_ENV') or define('YII_ENV', 'prod');

define('YII_VENDOR', '/var/www/html/riavrn-vendor');

require YII_VENDOR . '/autoload.php';
require YII_VENDOR . '/yiisoft/yii2/Yii.php';
require __DIR__ . '/../../common/config/bootstrap.php';
require __DIR__ . '/../config/bootstrap.php';

$config = yii\helpers\ArrayHelper::merge(
    require __DIR__ . '/../../common/config/main.php',
    require __DIR__ . '/../../common/config/main-local.php',
    require __DIR__ . '/../config/main.php',
    require __DIR__ . '/../config/main-local.php'
);



(new yii\web\Application($config))->run();
