B
B
Bombadile2017-10-09 11:48:57
Yii
Bombadile, 2017-10-09 11:48:57

Why is the redirect not working properly?

There is an init method in the controller with two almost identical pieces of code: one makes redirects for links with a plus code, and the second for links with a space code - all this is replaced with a regular plus sign and redirected to a link like site.ru/category+product. But only the redirect for a link with a space code works. For the plus code - 404, and it lands in the controller and even the redirect command works.

public function init()
    {
        parent::init();

        $url = Yii::$app->request->url;
        $host = Yii::$app->request->hostInfo;

        // redirect pages with %2B - plus
        if ((strpos($url, '%2B') !== false)) {
            $newUrl = str_replace('%2B', "+", $url);
            return $this->redirect($host.$newUrl, 301);
        }

        // redirect pages with %20 - space
        if ((strpos($url, '%20') !== false)) {
            $newUrl = str_replace('%20', "+", $url);
            return $this->redirect($host.$newUrl, 301);
        }
    }

What's the matter?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
Boris Korobkov, 2017-10-09
@BorisKorobkov

Where exactly do you insert the plus/space? If after the symbol "?", then everything works. If before - the router simply does not find a suitable controller.

M
Maxim Timofeev, 2017-10-09
@webinar

check what you have in $urlwhen there is a plus. But it seems to me that this should be done in htaccess, but the start of the yii assembly. Why waste resources.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question