Simple PHP API gives error 500 in Cpanel - php

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.

It's pretty obvious why it does not work. Look at your rules:
#root .htaccess
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
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.
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.

Related

Slim Framework .htaccess forward

There are several questions out there to that topic, but not a single one did match my question, because I am using Slim Framework 3.
One of my routes, for example, is /stats
When I make requests to http://localhost/slim-app/public/stats - it works.
But, I want to make requests to http://localhost/slim-app/stats - and that doesn't work. Slim gives me the error
Page Not Found The page you are looking for could not be found. Check
the address bar to ensure your URL is spelled correctly. If all else
fails, you can visit our home page at the link below.
My folder structure looks like that:
/slim-app
.htaccess
/logs
/public
index.php
.htaccess
/src
routes.php etc
/templates
/tests
/vendor
The .htaccess- File inside the slim-app folder looks like that:
RewriteEngine On
# Some hosts may require you to use the `RewriteBase` directive.
# If you need to use the `RewriteBase` directive, it should be the
# absolute physical path to the directory that contains this htaccess file.
#
#RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
The .htaccess- File inside the public- folder like that:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
</IfModule>
What should I change inside the htaccess- files to make it work? Thank you guys!

Remove public_html from url through htaccess

I have a legacy project on a server where the actual framework is in a folder above the public_html folder. So the directory structure is like this:
domainname.com
app/
framework/
public_html/
index.php
Now I want to place this on our own server. This is an application created by the hosting company. The root folder is default. So now my directory structure is like this:
default/
app/
framework/
public_html/
index.php
Now he's pointing to the default folder instead of the public_html folder. That's the only difference with the old version. So I've added a .htaccess file to the root folder default. The content of this file is:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /public_html/$1 [QSA,L]
</IfModule>
In my public_html folder I have a .htaccess file like this:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule . index.php [QSA,NS]
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule . /index.php [L,QSA,NS]
So when I go to my website it works. But when I click on a link I get: http://domainname.com/public_html/theclickedpage/.
But I don't want the public_html in my url. How can I fix this?
You may want to search for a way to make a virtual host. I know some of my webhost let me configure the vhost through my admin page. It will let your server consider the url without including the specified folder, but back in the stage it performs needed redirection.
You applied correct rule ,
but the problem is link for the solution of this issue you have to
remove public_html from
your all links and it will automatically redirect to your required
path without public_html.

Laravel called from ROOT outsite the /public folder. Getting NotFoundHttpException

I've searched through Web to solve my problem but non of the solutions works for me there, so after couple of hours of struggling with Routing module I've decided to ask you for a helping hand.
Problem
I am unable to access Laravel application from outside /public directory. I need to type localhost\projects\laravel\public in my browser but what I want is to use the URL withour /public. The main reason is because I use shared hosting and have no access to apache configuration file so I'm unable to create vhost.
Background
I've installed manually Laravel framework under the: **c:\xampp\htdocs\projects\laravel** and used composer to do the rest for me.
The routing has been set to: Route::get('/', 'HomeController#showWelcome');
In my project root directory **c:\xampp\htdocs\projects\laravel** I've put a .htacces file with the following content:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /projects/laravel/
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
In my Application Root dir c:\xampp\htdocs\projects\laravel\public I have .htaccess with the following code in it:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
When I type: localhost\projects\laravel\ I've got NotFoundHttpException
When I type: localhost\projects\laravel\public I've got welcome page
Any help would be appreciate, Thanks.
Finally I've got this thing working. There are two solutions that can be made.
One requires to move all -public content level-up
Second requires slightly changes in your shared hosting directory root structure - if allowed
Solution #1
The first that works for me that I didn't want to implement has been posted by #Wasim in this thread: Laravel 4 removing public from URL The solution is not save as the content core structure is in the same directory as application itself. This could cause some problems in future implementation.
You need to move all the content from public/ folder one level-up into project ROOT directory then replace internal paths in index.php file for correct onece. For security reasons this .htaccess file needs to be put into project ROOT directory
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Modified Laravel structure described above can be uploaded to your shared web hosting and the application should runs withour /public in yor http address.
Solution 2 (with no access to http.conf file on your shared hosting)
This solution does not requires form you to move the content of the /public folder one level-up but requires form you to have read and write access to ../ROOT directory in your shared hosting (../ROOT directory mostly contains public_html, public_ftp and other folders).
You need to move Laravel scructure into ../ROOT directory as follows:
app/
bootstrap/
vendors/
public_html/
public_ftp/
(...)
Files form /public folder goes to public_html
public_html/index.php
public_hmtl/.htaccess
public_html/packages/
(...)
Then modification for /bootstrap/paths.php is required for line with 'public' key:
'public' => DIR.'/../public' to 'public' => DIR.'/../public_html'
If someone has similar issue and this solution does not work please let me know, thanks.
If you want without public on uri, using composer from your laravel root. Use command php artisan serve and browse localhost:8000 from your browser. It will bring you to homepage.
Where did you get this line from?
RewriteRule ^(.*)$ public/$1 [L]
It's not part of Laravel. Try removing it or the whole .htaccess file in your Laravel root.

routing index.php with htaccess

I am running wamp on win7 just in case. I am trying to route all requests through a route.php to get clean URLs. Below is my htaccess
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_URI} !(auth|folder2) [NC]
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*) ./route.php?path=$1 [L]
As you can see the rule should apply to all files, and it did when I ran it on my old wamp installation. Now after copying the files to a new system, for some reason, the rule seems to work for all links except index.php. So if I try http://localhost/proj, I get no routing in the new system. Any ideas?
And before anyone asks, yes rewrite_module is on, and the htaccess is being read and executed cause it works for all other links like http://localhost/proj/users and also because if I try putting garbage values in the htaccess it throws a nice 'internal server error'.
I'm guessing you have an index.php in that folder, and this condition:
RewriteCond %{SCRIPT_FILENAME} !-f
...tells apache to perform the rewrite only if the file does not exist.
I would suggest or renaming the file, as you'll need that condition to route static assets like images, scripts, etc.
If there is no index.php (or index.htm[l]) in that folder, just define route.php as the default file in that directory. In your .htaccess:
DirectoryIndex route.php index.php index.html index.htm
(Be aware that this will also apply to any sub-directories as well)

Apache rewrite for Laravel /public

I am sorry about this, but my htdocs root is wrong and I can't change that. So I have to make it work in the /public folder.
I use the normal Laravel .htaccess file with the following rewrite:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
If I open http://kemtime2_neu.pr.domain.de/public I get redirected to http://kemtime2_neu.pr.domain.de/public/http://kemtime2_neu.pr.domain.de/public/login
How can I fix this?
I would love to make it work from http://kemtime2_neu.pr.domain.de/ but getting it to work with http://kemtime2_neu.pr.domain.de/public/ would be fine.
I use the 3 solution of this post and works fine:
http://web.archive.org/web/20130320184846/http://forums.laravel.io/viewtopic.php?id=1258
Solution 1 - Alternate installation path with symlink.
This is the preferred solution and in general an all-around good idea. It's possible to install your application to a folder unrelated to public_html/ and then symlink the public folder to the public_html/ path.
For example:
Install your application to /home/applications/mysite.com
Imagine that your DocumentRoot points to /var/www/vhosts/mysite.com/httpdocs
Remove the httpdocs folder from the mysite.com vhosts folder then connect the two with a symlink: ln -s /home/applications/mysite.com/public /var/www/vhosts/mysite.com/httpdocs
Solution 2 - .htaccess with mod_rewrite
This solution enables you to drop Laravel into your public folder then use a .htaccess file to redirect requests to the public folder. This solution places your application and core system code into a publicly accessible folder. This is not something that we encourage you to do with any PHP framework.
Step 1. Place Laravel in your document root folder.
Step 2. Place the following .htaccess file in your document root folder.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
Step 3. Make sure that you manually set your 'url' configuration in application/config/application.php otherwise Laravel will generate incorrect URLs. Make sure that each of your environments have the correct application.url configuration. For more information on environment-specific configurations see: http://laravel.com/docs/install#environments
Solution 3 - merge the public folder into the installation root
This solution places your application and core system code into a publicly accessible folder. This is not something that we encourage you to do with any PHP framework.
Copy the contents of the public/ folder into your Laravel installation folder then change this line in your index.php file from:
require '../paths.php';
to
require 'paths.php';
Keep in mind that any bundles, libraries, or other types of third-party code may not be designed to be publicly accessible.
Note: It's also important to note that your bundles/ and public/bundles/ directories will now conflict. When using this approach you may want to not use artisan's bundle:publish task without knowing exactly what your bundles want to publish.
I solved it partly. If I have a .htaccess in the root instead of /public with
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /public/index.php/$1 [L]
I can open http://kemtime2_neu.pr.domain.de/login but the images and css is still wrong. I need to check first if the files exist in /public. I think this is a new question.

Categories