0

I followed this tutorial to make a simple API with PHP. It works perfect on my computer. I added some other php files to the source folder and I added a composer package (Firebase/JWT) to the project, so a vendor folder and a couple composer files were created. Now the project folder looks like this:

/
- /plublic
-- .htaccess
-- index.php // requires src/functions.php
- /src
-- connection.php
-- functions.php // requires other src/.php files and JWT from composer
-- panel.php
-- responses.php
- /vendor
-- /composer // and files
-- /firebase // and files
-- autoload.php
.htaccess
composer.json
composer.lock

I uploaded this to my cpanel hosting (at api/) using a subdomain (api.mydomain.com). I made the subdomain with cpanel and first set the root folder to api/public but it didn't work (500 error). Then I tried pointing the subdomain to api/ but that didn't work either. What can I do? I think the problem is in the .htaccess files.

#root .htaccess
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]

#/public .htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

I don't know much about .htaccess or composer, so I didn't move anything of that. Thanks.

JCAguilera
  • 944
  • 1
  • 13
  • 32
  • To test you `.htaccess` theory, you can comment out each line one at a time .... – Zak Jun 25 '18 at 22:05
  • A 500 error can mean anything. You need to find the _actual_ error message. Without that, we would just be guessing. Check your servers error log. You can also change how PHP displays errors and tell it to show all errors directly on the screen (this is not something you want in production though, since it can show sensitive data, but during development, you should). Here's how to show all errors and warnings: https://stackoverflow.com/questions/5438060/showing-all-errors-and-warnings – M. Eriksson Jun 25 '18 at 22:08
  • @Zak I tried commenting the .htaccess but nothing happends. – JCAguilera Jun 25 '18 at 22:14
  • @MagnusEriksson tried that too, nothing :/ – JCAguilera Jun 25 '18 at 22:15
  • Make sure that you have `error_reporting(E_ALL);` while debugging and that you're checking the correct error log. As I said, without the error message, there's no way for us to know what's going on. It can be anything, like different/incompatible PHP-versions/configuration, missing extensions or something. Have you tried turning on `display_errors` while debugging? (You still need error reporting turned on.) – M. Eriksson Jun 25 '18 at 22:20
  • Did you check the logs of your server? Do they contain any information for the failing requests? – xabbuh Jun 26 '18 at 10:05
  • Ok, so I checked the logs of the server, it was just a permissions problem. – JCAguilera Jun 27 '18 at 14:24

1 Answers1

0

It's pretty obvious why it does not work. Look at your rules:

#root .htaccess
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
  1. The first Rule in your .htaccess file rewrites any request of the root, e.g. www.example.com or api.example.com/ to a subfolder /public.
  2. The second Rule complements this and rewrites any other request to the subdirectory /public, e.g. api.example.com/test to api.example.com/public/test

So, effectively, one of these two rules will always fire.

Since you have set the root of api.example.com already to /public it cannot find a /public/public subfolder; thus, you get an error 500.

To resolve this, you could try to remove both rules in the root .htaccess or you can set the root of /api not to the subfolder.

The public/.htaccess file has a typical single entry point configuration and should be fine.

Always use a fresh browser in incognito mode to test the changes.

wp78de
  • 18,207
  • 7
  • 43
  • 71