Slim Framework always return void 200OK - php

I have a web poject in a GoDaddy shared host. Is a webpage with an APIrest made with Slim Framework. This is the structure:
public_html/
- index.html (main webpage)
- css, js, ...
test/
include/
- database handler, connection, etc
rest/
- index.php (API)
- .htaccess
Slim/ (framework files)
The .htaccess is the original:
RewriteEngine On
# RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
The PHP version is the same on local server and the host (5.4).
The problem is that in local server works perfect, but when I put it in the host not. If I try the example:
$app->get('/hello/:name', function ($name) {
echo "Hello, $name";
});
I get a status 200 OK with void body. Any idea where is the problem? Thanks
Edit:
When change .htacces like this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
I always get status 404 not found

Related

How redirect to a static page in Public dir in a Symfony5 project

I have a simple static website.
On that I build a Symfony project to have an admin panel with easyadmin-bundle and an API to retreive data with ajax on the static page.
The project structure is like this:
bin/
config/
migrations/
public/
css/
js/
index.html
index.php
.htaccess
src/
templates/
Everythings works fine but I can only access to the page when I call :
myfakedomain.com/index.html
The page myfakedomain.com is returning a 404 error
What I would like to have is an automatic redirection from myfakedomain.com/ to myfakedomain.com/index.html
I think this can be done ine the .htaccess file. This is the current content of the .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI}::$0 ^(/.+)/(.*)::\2$
RewriteRule .* - [E=BASE:%1]
# Sets the HTTP_AUTHORIZATION header removed by Apache
RewriteCond %{HTTP:Authorization} .+
RewriteRule ^ - [E=HTTP_AUTHORIZATION:%0]
RewriteCond %{ENV:REDIRECT_STATUS} =""
RewriteRule ^index\.php(?:/(.*)|$) %{ENV:BASE}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ %{ENV:BASE}/index.php [L]
</IfModule>
This project is deployed on heroku so I create a Procfile
web: vendor/bin/heroku-php-apache2 public/
May it come from that ?
It comes from your .htacess which redirects all requests to index.php.
Option1 - using Apache (I don't like this method because of side effects)
Exclude the existing files from the redirection.
Declare index.html as the default file instead of index.php (when calling .
Tips: apache-pack can help you. It contains an up-to-date default configuration
Option2 - using Symfony (Recommanded way):
move index.html to template/index.html
Create a controller returning a response rendering the template files
# src/Controller/HomeController.php
class HomeController extends AbstractController
{
#[Route(name:'home', '/')]
public function index (): Response
{
return $this->render('index.html');
}
}

Issues Hosting PHP app with Slim Framework on VPS

I have a PHP application that is using Slim framework for APIs. Everything is working fine on localhost...however... not on the VPS..I am using Digital Ocean.
Here is my project structure.... Just wanted to ensure whether it is right.
ROOT DIRECTORY
- app
-- admin - index.php (Main App Homepage)
- .htaccess
- Other CSS & javaScript
API (This is Slim Project)
-- includes
-- lib
-- v1
.htaccess (htaccess of Slim)
-- index.html
MAIN ISSUE:
On running the app... say login page...it makes a request to the Slim ...the api responds back with the json...but it shows in the browser rather than showing the html formatted response.
Not sure what is missing.
.htaccess for Slim
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ %{ENV:BASE}index.php [QSA,L]
.htaccess for main app
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]
</IfModule>
Did you change the content-type to application/json ?
Check this, Slim has a simple way to return a json, this way :
$data = array('name' => 'Bob', 'age' => 40);
$newResponse = $oldResponse->withJson($data);

Redirect to php script using htaccess

For a web-application I would like to rewrite the URL.
From:
http://example.com/api/logger/all
To:
http://example.com/api/index.php/logger/all
So I tried the following:
RewriteEngine On
RewriteCond %{REQUEST_URI} !/index.php/
RewriteRule ^api(.*)$ api/index.php$1 [L]
I also testet it successfully with http://htaccess.madewithlove.be/.
On the server I get a 404.
My project struct looks like following:
--api
----index.php
--htaccess
--index.html
Update:
I found a solution for external redirects.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !(index\.php)
RewriteRule ^api/(.*)$ /api/index.php/$1 [R=302]
But I need a internal redirect and if remove [R=302] I get 404 with "No input file specified." as response.
Following htaccess file works for
http://example.com/logger/all.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !(index\.php)
RewriteRule ^(.*)$ /api/index.php/$1
But adding api again results in "No input file specified."
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !(index\.php)
RewriteRule ^api(.*)$ /api/index.php/$1
mod_rewrite is a double-edged sword that is incredibly easy to misuse.
Apache 2.2.16 and later provide the extremely useful but surprisingly seldom used FallbackResource directive.
So use the following layout:
--api
----.htaccess
----index.php
--index.html
And use the following contents for your .htaccess file:
FallbackResource /api/index.php
The main difference with the mod_rewrite based solutions is that there is no substitution in the URI, so you have to parse the URI in PHP using $_SERVER['REQUEST_URI'].
For example here, if you access http://example.com/api/logger/all, your index.php file will see the following value:
echo $_SERVER['REQUEST_URI']; // prints /api/logger/all
The main interest of putting the .htaccess file in the api directory is that it ensures that only URLs under the /api prefix are handled by FallbackResource. Any URL that would cause a 404 error in the document root folder will not trigger a call to /api/index.php.

.htaccess rules for pushstate support

I started an app based on Angular.js and Silex for the server side.
I would use real URL (without hash) but it's not working.
I have activate pushstate on angular with the following line :
$locationProvider.html5Mode(true);
but I don't know how to configure server. Actually when I try to access to localhost/test angular did nothing but silex said : *No route found for "GET /test" ..
my htaccess is like it :
<IfModule mod_rewrite.c>
Options +FollowSymLinks
IndexIgnore */*
RewriteEngine On
RewriteBase /web
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) index.php [L]
Structure of my project is Like it
|--app/
|----app.php
|----controllers/
|-------MainController.php
|--web/
|----index.php
|----js/
|---------app.js
...
Thanks you for your help
edit :
route declaration :
config(['$routeProvider, $locationProvider', function($routeProvider, $locationProvider) {
$locationProvider.html5Mode(true);
$routeProvider.when('/test',{
templateUrl : '/cuisine',
controller : 'CookController'
});
}
I am using this on my site, and its working , please give it try. Otherwise i suggest you turn on mod_rewrite logging in apache and look to logs
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]
RewriteRule ^ /index.html
Your problem is with RewriteBase. The rewrite base is based on URI folder and you're requesting /test not /web/test, so your rewrite base should be /
I'm just guessing but I think your document root is /path/to/your/project/web (it should be this, only the web directory is meant to be public!) so your .htaccess shouldn't have the RewriteBase on your case.

Fatfree Framework error 404 on every route

This is working localy on my WAMP server, but when I tried to use it on my host it always gets an error:
HTTP 404 (GET /)
• teste/index.php:17 Base->run()
You can see the error here: http://rafaelmsantos.com/teste/
I don't have a clue whats going on, I've tried different .htaccess but it display always the same error.
.htaccess
# Enable rewrite engine and route requests to framework
RewriteEngine On
# Some servers require you to specify the `RewriteBase` directive
# In such cases, it should be the path (relative to the document root)
# containing this .htaccess file
#
# RewriteBase /
RewriteRule ^(lib|tmp)\/|\.(ini)$ - [R=404]
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L,QSA]
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
config.ini
[globals]
AUTOLOAD=public/pages/
DB.dns="mysql:host=localhost; dbname=lod; port=3306;"
DB.user="root"
DB.password=""
DEBUG=3
UI=assets/
index.php
<?php
$lod = require('lib/base.php');
$lod->config('config.ini');
// HELPERS DEVELOPED BY ME
require_once 'helpers/base_helper.php';
//*-----------------------------------------------------------------*/
//* PÁGINAS */
//*-----------------------------------------------------------------*/
$lod->route('GET /', 'PagesController->index');
$lod->route('GET /project/#page', 'PagesController->index');
$lod->run();
And my folder structure:
try to set the RewriteBase in your .htaccess (as the comment above said):
RewriteBase /teste/
Turns out I had to autoload in another way,
insted of AUTOLOAD=public/pages/ on config.ini, I had to use $lod->set('AUTOLOAD','public/pages/'); after the require_once and before the routes.

Categories