E
E
Eugene2018-02-14 15:28:56
Yii
Eugene, 2018-02-14 15:28:56

Why is Yii2 CNC not working?

I am learning the Yii2 basic framework. And it seems like everything is fine, but the CNC did not work at the start, and then I decided to give up on this problem and study further, but now I decided to return.
The webserver I am using is wamp64.
The site is in the site folder.
And that root directory is localhost/site/web
I uncommented

'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'rules' => [
            ],
        ],

Added web redirect to root
RewriteEngine On
RewriteRule ^(.+)?$ /web/$1

On the web, another .hcaccess file
RewriteBase /

RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d

RewriteRule . index.php

Figs with unloaded styles and scripts, but when you go to another page, this link is created.
http://localhost/site/web/site/contact
Where else does it pin the site folder. How can I fix this so that the CNC works correctly?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry, 2018-02-15
@evgen9586

Rename the site directory, come up with a name for this local project, because in views there is a site directory that the SiteController controller accesses.
Let the new name be mysite.
In this directory you will place your entire yii2 project.
Then, in the root of your project, put .htaccess with the following content:

<IfModule mod_rewrite.c>
  Options +FollowSymlinks
  RewriteEngine on
</IfModule>

<IfModule mod_rewrite.c>
   RewriteCond %{REQUEST_URI} ^/.*
   RewriteRule ^(.*)$ web/$1 [L]

   RewriteCond %{REQUEST_URI} !^/web/
   RewriteCond %{REQUEST_FILENAME} !-f [OR]
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteRule ^.*$ web/index.php

</IfModule>

And in the web project directory, leave this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php

Now, in the config/web.php file, do the following:
use \yii\web\Request;
$baseUrl = str_replace('/web', '', (new Request)->getBaseUrl());
return [
   'components' => [
       'request' => [
           'baseUrl' => $baseUrl,
       ],
   ],

];

This will give you the ability to copy the configuration file to your different projects without worrying about the name of the directory in which the project will be located.
And in urlManager write the first, main rule
'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'rules' => [
                  '' => 'site/index'
            ],
        ],

Then when you go to the address localhost/mysite, you will have to open the main page of your project, without "web" in the address bar.

I
Ilya Beloborodov, 2018-02-14
@kowap

'components' => [
       'request' => [
            'baseUrl' => '',
        ],
]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question