App always returns 404 after deployment to a subfolder in remote host

I implemented my slim app and tested on localhost and everything seems to be working fine. I want to test the app on a shared host so that other developers can also access the apis.
I decided to deploy it to my shared host but not directly under the public_html rather in a subfolder called public_html/AppName.The reason is that I don’t want to lose my homepage accessible with the public.htlm in the public_html folder.

With FTP I uploaded all my files including the vendor folder so that I don’t need to install composer on the remote machine.

The problem is I can’t access any of my routes since it returns 404.
If call the root route /, I see the listing of my files under rather than the start page of the app.

I would appreciate if anyone could help me with this.
Kind regards
Ilker

Are you using Apache, if so what does your .htaccess file in the subfolder look like?

Thanks for replying so fast. Yes it’s an apache web server.Actually there is no .htaccess file in public_html/subfolder. I only have one in the public_html.I can pretty new in Backend development so I apologise for my question but what should I need to write in .htaccess?

You will need one in there to route requests into the front controller. See Apache Configuration in the Slim Docs for an example. (There could be other issues as well, but that is a step in the right direction.)

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

didn’t help me a either.BTW the nested public_html is not listed when I call www.mysite.com/subFolder. This is the place where index.php resides.Could it be related with that ?

You shouldn’t need the public_html part in the RewriteRule unless that is actually in your public space.

Where is your front controller (index.php) relative to your document root? Based on what you’ve written above, I’d assume it is in AppName/public. Is that correct?

The actual place of the index.php is public_html/subFolder/public_html/index.php. I renamed the original public folder to public_html inside the slim project.

I assume the initial public_html is your document root? If so, inside SubFolder/public_html/, alongside the index.php file, you will need an .htaccess file with:

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

Then you would call the app from http://example.com/SubFolder/public_html/

If that doesn’t work, look in the server’s access log to see what is being called, or in the error log to see the error details.

I tried an entry point http://www.mywebsite.com/fruitApp/products and got http status 500 , according to error logs of the server
[Thu Jun 01 15:45:46 2017] [warn] [client 212.114.132.66] mod_fcgid: stderr: PHP Parse error: syntax error, unexpected ‘class’ (T_CLASS), expecting identifier (T_STRING) or variable (T_VARIABLE) or ‘{’ or ‘$’ in /home/ilkerbal/public_html/fruitApp/vendor/slim/slim/Slim/Container.php on line 149.

the corresponding router is $app->get(’/products’, ‘ProductsController:getProducts’);

That seems closer. What version of PHP is the server running?

It was PHP 5.6.30 on the local machine and on the server PHP 5.5.36.
I upgraded my php on the server to 5.6 and got rid of the problem.

Edit:
I figured out that my root is currently http://www.mywebpage.com/fruitApp/public_html/ so I can access my products http://www.mywebpage.com/fruitApp/public_html/products

Is it possible to get rid of the /public_html/ so that I can access them directly with http://www.mywebpage.com/fruitApp/products?

Sure, move everything in public_html up a directory level. Your index.php would then probably needed to be updated to point to the new relative location of the autoloader. Something else to keep in mind is that all of the files outside of your public directory should not be accessible from the outside as a security precaution, something that may or may not be possible with your shared host.

In one of your posts in the forum you advised to keep source file one level above the public_html folder. Do you think it is better practice to have it like this if I am allowed to write to root folder?

Best practice is to only have the public (or in your case public_html) folder exposed to the web. In the Slim-Skeleton example only the public folder would be exposed to the web as it would be the document root. All other files and directories wouldn’t be directly accessible.

1 Like

I solved adding entire path in routes:

$app->group('/fruitApp', function() {
    $this->get('/product', 'Controller:Product');
});

I was api in same situation, when I moved to production the group function has just ‘/’, and always return 404. After some investigation I saw that routes are always with full path and add ‘/fruitApp’ (all path) to the route group.

My servers are nginx.

Thank you for your all tips. FInally I cleaned up everything and I am ready to go =)

My Problem was with routes was that index.php had a different place in the application when I uploaded to my host.Best practice is mimicking the remote folder structure when you develop on your local machine to avoid such problems.