Answer the question
In order to leave comments, you need to log in
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' => [
'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
Good morning.
Try like this
RewriteCond %{REQUEST_URI} ^/(api)
RewriteRule ^api(/.*)?$ api/web/$1 [L]
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 questionAsk a Question
731 491 924 answers to any question