Deploy Slim project to Hostgator using subdomain

I have a very simple Slim app that works fine on my Ubuntu machine but I get an error 500 when I deploy it to a Hostgator shared account using a subdomain.

I suspect the problem lies in my .htaccess file.

The structure of my files is:

project/
├──api/
│   ├── .htaccess
│   ├── services.php
├──src/
    ├──vendor/

My Slim app (services.php):

<?php
    use \Psr\Http\Message\ServerRequestInterface as Request;
	use \Psr\Http\Message\ResponseInterface as Response;
    require_once '../src/vendor/autoload.php';

    $c = new \Slim\Container();

    $c['notFoundHandler'] = function ($c) {
	    return function ($request, $response) use ($c) {
	        return $c['response']
	            ->withStatus(404)
	            ->withHeader('Content-Type', 'text/html')
	            ->write('Invalid endpoint');
	    };
	};

    $app = new \Slim\App($c);	
	$app->post('/myEndpoint', function (Request $request, Response $response, array $args) {
		if($request->getHeader("key") != null && $request->getHeader("key")[0] == "123456789="){
			$reqdata = $request->getParsedBody();
			$data = $reqdata['data'];
			return json_encode('{"data":'.$data.',"response":"OK","errorcode":"0"}');		
		} else {
			return '{"data":"","response":"Access denied","errorcode":"9999"}';
		}
	});

	$app->run();

My .htaccess file:

    <IfModule mod_rewrite.c>
      RewriteEngine on
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteRule . services.php [QSA,L]
    </IfModule>
    # Use PHP71 as default
    AddHandler application/x-httpd-php71 .php
    <IfModule mod_suphp.c>
      suPHP_ConfigPath /opt/php71/lib
    </IfModule>

I created a subdomain (api.mydomain.com) with project/api/ as document root.

When I make a POST request in my local installation to

127.0.0.1/api/services.php/myEndpoint

works as expected and returns the data. However, when I make the same request to my Hostgator subdomain:

api.mydomain.com/services.php/myEndpoint

I get the error 500.

Note that if I add a simple test.php file to my api/ directory that only prints something, e.g.

    <?php
      echo "OK"
    ?>

and I go to api.mydomain.com/test.php, it works fine and prints the string.

What do I need to change or add in my .htaccess for my slim app to work?

What is the error message, can you provide that from your error logs?

Try adding

RewriteBase /

to <IfModule mod_rewrite.c>

And make a POST request at the URL api.mydomain.com/myEndpoint

I don’t get an error.log file like when a PHP file fails. The only error I get is the answer from the POST request. A rendered version looks like

Screenshot%20from%202019-05-24%2010-21-17

I tried it but didn’t fix it. Still get the same answer. I added that line after RewriteEngine on, so my .htaccess now looks like

<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteBase /
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule . services.php [QSA,L]
</IfModule>
# Use PHP71 as default
AddHandler application/x-httpd-php71 .php
<IfModule mod_suphp.c>
    suPHP_ConfigPath /opt/php71/lib
</IfModule>

Did you mean

RewriteRule ^ services.php [QSA,L]

Instead of

RewriteRule . services.php [QSA,L]

You should get access to Apache’s error log, as that would tell you exactly what is going on, rather than guessing.

Thank you! That did the trick.

I checked the Apache error log but, at least the log Hostgator provides, shows nothing.

Also, another thing I had to do is move my project directory to the public_html folder. Then, I created the subdomain again and pointed it to that directory.

I’m still very ignorant about how .htaccess works. What’s the difference between

RewriteRule ^ services.php [QSA,L]

and

RewriteRule . services.php [QSA,L]

Thank you so much.