M
M
Maxim2018-06-06 03:05:58
Yii
Maxim, 2018-06-06 03:05:58

Yii how to setup htaccess for api?

Routing does not work correctly or the configuration is not correct.
When going to site.ru/api/ displays root folders
When going to site.ru/api/web/ - displays site.ru/site/index/
When going to site.ru/site/index/ - displays 404.
I can't figure out what's wrong so in my data. For frontend and backend it works as it should, but for api it doesn't want to...
htaccess

<IfModule mod_rewrite.c>
    Options +FollowSymlinks
    RewriteEngine On
</IfModule>
 
<IfModule mod_rewrite.c>
    # deal with admin first
    RewriteCond %{REQUEST_URI} ^/(admin)
    RewriteRule ^admin/assets/(.*)$ backend/web/assets/$1 [L]
    RewriteRule ^admin/css/(.*)$ backend/web/css/$1 [L]

    RewriteCond %{REQUEST_URI} ^/(api)
    RewriteRule ^.*$ api/web/index.php [L]

    RewriteCond %{REQUEST_URI} !^/backend/web/(assets|css)/
    RewriteCond %{REQUEST_URI} ^/(admin)
    RewriteRule ^.*$ backend/web/index.php [L]

    RewriteCond %{REQUEST_URI} ^/(assets|css|js|images)
    RewriteRule ^assets/(.*)$ frontend/web/assets/$1 [L]
    RewriteRule ^css/(.*)$ frontend/web/css/$1 [L]
    RewriteRule ^js/(.*)$ frontend/web/js/$1 [L]
    RewriteRule ^images/(.*)$ frontend/web/images/$1 [L]
    RewriteRule ^(.*)$ frontend/web/$1 [L]

    RewriteCond %{REQUEST_URI} !^/(frontend|backend)/web/(assets|css|js)/
    RewriteCond %{REQUEST_URI} !index.php
    RewriteCond %{REQUEST_FILENAME} !-f [OR]
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^.*$ frontend/web/index.php
</IfModule>

urlManager
'urlManager' => [
            'enablePrettyUrl' => true,
            'enableStrictParsing' => false,
            'showScriptName' => false,
            'suffix' => '/',
            'rules' => [
                //'/' => 'site/index',
                'auth' => 'site/login',

                'GET profile' => 'profile/index',
                'PUT,PATCH profile' => 'profile/update',

                ['class' => 'yii\rest\UrlRule', 'controller' => 'post'],
            ],
        ],

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry, 2018-06-06
@myks92

Good morning.
Try like this

RewriteCond %{REQUEST_URI} ^/(api)
RewriteRule ^api(/.*)?$ api/web/$1 [L]

S
sergeysm128, 2019-08-16
@sergeysm128

Add
'request' => [
'baseUrl' => '/api',
] to the api config.
In this case, the redirect will work exactly the same as for the admin panel

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question