Question

For some reason when I deploy my API using Restler's API Explorer (a fork of Swagger UI) to production it gives me a general 404 error when I load:

/api/explorer

When I am more explicit and state:

/api/explorer/index.html

It loads the framing for the page but then reports "404 : Not Found ../resources.json" in red text below the header:

enter image description here

I'm fairly certain there's something environmental flaring up as the same files locally work. I also checked the .htaccess file in the /api/explorer directory and it looks right to me:

DirectoryIndex index.php
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^$ index.html [QSA,L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.html [QSA,L]
</IfModule>

Any help would be appreciated.

Was it helpful?

Solution

It turns out all the problems were down to a mod_rewrite problem. I'm still not 100% on WHY this problem showed up in one environment but not others with precisely the same httpd.conf and .htaccess. Oh well, the solution is to be explicit in the rewrite rule about your base url using the RewriteBase directive.

In the API directory I now have the following .htaccess file:

DirectoryIndex index.php
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /api
    RewriteRule ^$ index.php [QSA,L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>

In the API-Explorer directory I now have the following .htaccess:

DirectoryIndex index.php
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /api/explorer
    RewriteRule ^$ index.html [QSA,L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.html [QSA,L]
</IfModule>

In order to troubleshoot this problem one invaluable tip that I came across is turning on Apache's mod_rewrite logging.

# Adding logging for Rewrites
RewriteLog logs/rewrite.log
RewriteLogLevel 2

Put this in any globally scoped area of httpd.conf; I put it around the entries that were already there about logging but you can also just put it at the end if you like that better. In addition, the basics around rewrite troubleshooting includes (apologies if this basic):

  1. make sure that your httpd.conf has AllowOverride All and Options FollowSymLinks set for the directories you serving Restler out of.
  2. You can put all of the configuration into httpd.conf and avoid .htaccess files altogether (and it's a bit faster that way too) but if you do that remember that httpd.conf has absolute url referencing versus .htaccess's relative.

OTHER TIPS

You can check the ReadMe file on the Restler Github page https://github.com/Luracast/Restler-API-Explorer#readme

Did you add the 'Luracast\Restler\Resources' class to create resources.json at API Root?

In your own index.php, the addAPIClass section should look like this:

$r = new Restler();

$r->addAPIClass('Luracast\\Restler\\Resources'); //this creates resources.json at API Root

// ... your own addApiClass

$r->handle();

It is in the documentation under "Use", but I had trouble figuring this out as well...

Make sure you have cache directory with write permission in your api root

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top