O
O
oleg2016-08-10 16:11:58
Yii
oleg, 2016-08-10 16:11:58

How to make a redirect without changing the url?

The bottom line is this:
there is a url with GET. http://test.ru/?s%5bcategory%5d=8
it is necessary for the server to receive it with such a request: test.ru/komnata
what rule to write in htaccess?
Those. so that the server receives get but does not display it in the url
No regulars are needed, I have only a few static pages with such key-values
​​For now, so far:

RewriteCond %{REQUEST_URI} /studiya/
RewriteRule ^(.*)$ /?s\[category\]=4\&s\[kinds\]\[\]=7 [R=301,L]

It works, but in the url it's not pretty. Geth is output. Those. it's just a redirect. But you need SEO.
By the way, yii2 is used - the CNC urlManager is active, but I don’t know how to push get there.
Thanks in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Igor Vasiliev, 2016-08-11
@donotar

Knowing your version, I explain that there are roles in the urlManager that must be specified. If get requests are used in the roles, then you need to take into account whether it is a number or a string, if it is a number, set the value to 'd' (digital) if it is a string, specify the value 'w' (word), as in the example:

'urlManager'=> [
            'enablePrettyUrl' => true,
            'showScriptName' => false,            
                'rules'=> [
// возвратит название домена, без слэша ( 1 )
                    '' => 'site/index',
// возвратит экшен без названия папки контроллера ( 2 )
                    '<action:(login|logout|new|index|contact)>' => 'site/<action>',
                    '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
                    '<controller:\w+>/<id:\d+>'=>'<controller>/view',
                    '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
// возвратит get запрос с ссылкой, у которой переменная $link отобразит строку ( 3 )
                    'site/news/<link:\w+>' => 'site/news',
                ],            
        ],

path to these parameters /frontend/config/main.php
in the controller /frontend/controllers/SiteController.php
public function actionNews($link = '') {
            // делаем get запрос
            $get = News::find()->where(['link' => $link])->one();
            return $this->render('news', ['link' => $link, 'get' => $get,]); 
  }

of course, do not forget to specify the links for the class, and create the news.php file in the view.
In the file itself, all get-variables will look something like this: it is
clear that $get is a variable making a request to the database, and header is a variable and the name of a column in the table The database from which the value is passed by the $link variable and not $id !!!
You get something like:
// это для примера, писать не надо
<?php 
$link = 'first';
?>
http://mysite.host/frontend/web/site/news/first

If you want to remove /frontend/web/, you will have to change session and request , this can lead to the loss of directories for many styles and scripts, and will block the oxygen of the backend section, but you can make a module, create your own layout and separate this section in the frontend so that it is like a backend, at the same time, the folders with the admin panel will be in the frontend, the operation is complex and requires knowledge of the Yii2 architecture, I draw knowledge from the documentation , although it is in English, but there are familiar Yii2 code fragments and even examples are described and how and what to apply.
I ended up with links like:
mysite.host/site/news/first - on get request, case ( 3 )
and
mysite.host/news- when displaying a page from an action. Case ( 2 )
--
See above.
I wish you success ))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question