REST API too many redirects

I have created a REST API in the advanced template following a similar approach to this. When I make any requests to the API I get a 500 internal server error and the apache logs show there’s a “too many redirects” error: Request exceeded the limit of 10 internal redirects due to probable configuration error.

I have implemented the same API but using the basic template and it works just fine. The API is configured basically exactly the same in both versions so I think it’s possible that my apache config is the problem. I configured the api endpoint in my .../sites-enabled/dev.site.conf in the exact same way as I configured my frontend and backend endpoints which work perfectly.

My apache configuration and main.php are below, if anyone wants to see any other files just let me know and I’ll post them, didn’t want to just vomit code all over the page unnecessarily. I’ve spent a good 2 full days on this so far so I would really appreciate any help in getting it figured out, thanks :D

.../sites-enabled/dev.site.conf:


<VirtualHost *:80>

    ServerName dev.site


    RewriteEngine on

    RewriteCond %{REQUEST_URI} !^/(backend/web|admin|api/web|api)

    RewriteRule !^/frontend/web /frontend/web%{REQUEST_URI} [L]


    RewriteCond %{REQUEST_URI} ^/admin$

    RewriteRule ^/admin /backend/web/index.php [L]

    RewriteCond %{REQUEST_URI} ^/admin

    RewriteRule ^/admin(.*) /backend/web$1 [L]


    RewriteCond %{REQUEST_URI} ^/api$

    RewriteRule ^/api /api/web/index.php [L]

    RewriteCond %{REQUEST_URI} ^/api

    RewriteRule ^/api(.*) /api/web$1 [L]


    DocumentRoot path/to/project

    <Directory />

        Options FollowSymLinks

        AllowOverride None

        AddDefaultCharset utf-8

    </Directory>


    <Directory path/to/project/frontend/web>

        RewriteEngine on

        # if a directory or a file exists, use the request directly

        RewriteCond %{REQUEST_FILENAME} !-f

        RewriteCond %{REQUEST_FILENAME} !-d

        # otherwise forward the request to index.php

        RewriteRule . index.php


        # Apache 2.4

        Require all granted


        ## Apache 2.2

        #Order Allow,Deny

        #Allow from all

    </Directory>


    <Directory path/to/project/backend/web>

        RewriteEngine on

        # if a directory or a file exists, use the request directly

        RewriteCond %{REQUEST_FILENAME} !-f

        RewriteCond %{REQUEST_FILENAME} !-d

        # otherwise forward the request to index.php

        RewriteRule . index.php


        # Apache 2.4

        Require all granted


        ## Apache 2.2

        #Order Allow,Deny

        #Allow from all

    </Directory>

    

    <Directory path/to/project/api/web>

        RewriteEngine on

        # if a directory or a file exists, use the request directly

        RewriteCond %{REQUEST_FILENAME} !-f

        RewriteCond %{REQUEST_FILENAME} !-d

        # otherwise forward the request to index.php

        RewriteRule . index.php


        # Apache 2.4

        Require all granted


        ## Apache 2.2

        #Order Allow,Deny

        #Allow from all

    </Directory>


    <FilesMatch \.(htaccess|htpasswd|svn|git)>

        Deny from all

        Satisfy All

    </FilesMatch>

</VirtualHost>

And /api/config/main.php:


<?php

$params = array_merge(

    require(__DIR__ . '/../../common/config/params.php'),

    require(__DIR__ . '/../../common/config/params-local.php'),

    require(__DIR__ . '/params.php'),

    require(__DIR__ . '/params-local.php')

);


return [

    'id' => 'app-api',

    'basePath' => dirname(__DIR__),

    'bootstrap' => ['log'],

    'modules' => [

        'v1' => [

            'basePath' => '@app/modules/v1',

            'class' => 'api\modules\v1\Module'

        ]

    ],

    'components' => [

        'db' => [

            'class' => 'yii\db\Connection',

            'dsn' => 'mysql:host=localhost;dbname=anygym',

            'username' => 'root',

            'password' => 'root',

            'charset' => 'utf8',

        ],

        'request' => [

            'parsers' => [

                'application/json' => 'yii\web\JsonParser',

            ]

        ],

        'cache' => [

            'class' => 'yii\caching\FileCache',

        ],

        'user' => [

            'identityClass' => 'common\models\User',

            'enableAutoLogin' => true,

        ],

        'errorHandler' => [

            'errorAction' => 'site/error',

        ],

        'log' => [

            'traceLevel' => YII_DEBUG ? 3 : 0,

            'targets' => [

                [

                    'class' => 'yii\log\FileTarget',

                    'levels' => ['error', 'warning'],

                ],

            ],

        ],

        'urlManager' => [

            'enablePrettyUrl' => true,

            'enableStrictParsing' => true,

            'showScriptName' => false,

            'rules' => [

                [

                    'class' => 'yii\rest\UrlRule', 

                    'controller' => 'v1/gym',

                    'tokens' => [

                        '{id}' => '<id:\\w+>'

                    ]

                    

                ],

            ],        

        ],

    ],

    'params' => $params,

];

"/api" will be redirected to "/api/web/index.php" by the 1st rule.

"/api/web/index.php" will be redirected to "/api/web/web/index.php" by the 2nd rule.

"/api/web/web/index.php" will be redirected to "/api/web/web/web/index.php" by the 2nd rule.

:)

My knowledge of rewrite rules is very limited and I’m not doubting you, just trying to understand better, so if that is the case then why does the backend not behave in the same way?

"/admin" will be redirected to "/backend/web/index.php" by the 1st rule.

“/admin/web/index.php” will be redirected to “/backend/web/index.php” by the 2nd rule. And that’s all.

Since “admin” and “backend” are different, there’s no possibility that the same rule could be applied multiple times.

:blink: I don’t know how I didn’t see that, I think I had been looking at it for so long that I couldn’t see the wood for the trees! Thanks!