RESTful API .htaccess nightmare

Hello,

I’m trying to setup my first API in Modx, I’ve followed all the steps in the documentation. When I access the /rest/ folder in the browser the error indicates the API is working fine. However, when I try to access /rest/resources nothing happens and the main homepage loads instead.

Something tells me its related to the .htaccess configuration. I see different information across the web, some say the main root .htaccess is to be modified, others say to create a new .htaccess file inside the /rest/ folder. I’ve tried both but nothing seems to work.

Can someone please point me in the right direction? Here’s what my code looks like

/rest/index.php

<?php
// Boot up MODX
require_once dirname(dirname(__FILE__)) . '/config.core.php';
require_once MODX_CORE_PATH . 'model/modx/modx.class.php';
$modx = new modX();
$modx->initialize('web');
$modx->getService('error', 'error.modError', '', '');
// Boot up any service classes or packages (models) you will need
$path = $modx->getOption(
    'mypackage.core_path',
    null,
    $modx->getOption('core_path') . 'components/mypackage/'
) . 'model/mypackage/';
$modx->getService('mypackage', 'myPackage', $path);
// Load the modRestService class and pass it some basic configuration
$rest = $modx->getService('rest', 'rest.modRestService', '', array(
    'basePath' => dirname(__FILE__) . '/Controllers/',
    'controllerClassSeparator' => '',
    'controllerClassPrefix' => 'MyController',
    'xmlRootNode' => 'response',
));
// Prepare the request
$rest->prepare();
// Make sure the user has the proper permissions, send the user a 401 error if not
if (!$rest->checkPermissions()) {
    $rest->sendUnauthorized(true);
}
// Run the request
$rest->process();

/rest/Controllers/Resources.php

<?php
class MyControllerResources extends modRestController
{
    public $classKey = 'modResource';
    public $defaultSortField = 'sortorder';
    public $defaultSortDirection = 'ASC';
}

root .htaccess

#Options +FollowSymlinks
RewriteEngine On
RewriteBase /

# avoid IO on subdir: extras, media, themes, images, assets, connectors and manager
RewriteCond %{REQUEST_URI} !^(/core/|/images/|/assets/|/connectors/|/manager/) [NC]

# The Friendly URLs part
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

#REST server
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^(.*)$ rest/index.php?_rest=$1 [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ rest/index.php [QSA,NC,L]

You need to access:

/rest/resources

instead of

/rest/Controllers/Resources.php

I do wonder if your htaccess conflicts between the standard MODX rewrite and the one for the the REST server, but what you did does seem to match the documentation…

Thanks Mark, yes that’s actually what I did “/rest/resources”, not /rest/Controllers/Resources.php. I’ll edit/update my question?

you have to have the .htaccess within the /rest folder

I have it that way
fbuch/.htaccess at master · Bruno17/fbuch (github.com)

I think it depends whether you want to run a headless CMS (MODX is just an API) or a normal website that also has an API.

I believe the problem in your case is that the RewriteRule

RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

redirects everything to index.php and the other rules further down are never executed.


If you get too frustrated with this approach, maybe try QuickApi.

This is what works for me:

Put the .htaccess file with this content into the rest-folder.

RewriteEngine On
RewriteBase /rest/

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^(.*)$ index.php?_rest=$1 [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ index.php [QSA,NC,L]

Don’t change the .htaccess in the main root.

1 Like