Heroku silex route show 404 except "/" - php

This piece of code was from an example at heroku. But except the route /, anything else I add does not work. It shows 404:
The requested URL /e was not found on this server.
$app->match('/', function(Symfony\Component\HttpFoundation\Request $request) use ($app) {
return $app['twig']->render('index.twig');
});
$app->match("/dump", function(Symfony\Component\HttpFoundation\Request $request) use ($app) {
return new Response('Thank you for your feedback!', 201);
})->bind("dump");
$app->get("/t", function() use ($app) {
return new Response('Thank you for your feedback!', 201);
})->bind("t");
$app->get("/d", function() {
return new Response('Thank you for your feedback!', 201);
})->bind("d");
$app->get("/e", function() {
return new Response('Thank you for your feedback!', 201);
});
$app->run();
Edit 1
I deployed it directly on heroku server (heroku auto build on each push to master branch)
Edit 2
my workspace:
bin\worker.php
www\index.php <== the snippet is from this file
www\list.php
apache_app.conf
app.php <== basic init, return Silex\Application $app
Procfile
The content of apache_app.conf is copied from this link.
RewriteEngine On
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^app\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]
RewriteRule .? %{ENV:BASE}/app.php [L]
I figured that I need to change the apache config somehow, but I don't understand htaccess syntax.

For apache2, you can add a .htaccess file within "/web"
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
#RewriteBase /path/to/app
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
</IfModule>
and set your Procfile as:
web: vendor/bin/heroku-php-apache2 web/
For nginx, create a nginx conf file with content:
location / {
# try to serve file directly, fallback to rewrite
try_files $uri #rewriteapp;
}
location #rewriteapp {
# rewrite all to index.php
rewrite ^(.*)$ /index.php/$1 last;
}
location ~ ^/(index|index_dev)\.php(/|$) {
try_files #heroku-fcgi #heroku-fcgi;
internal;
}
and set Procfile as:
web: vendor/bin/heroku-php-nginx -C server_conf/nginx.conf web/

It's very simple, just install apache :
composer require apache-pack

I had the same issue, and it stumped me for weeks... to the point where I just started using / as my only route and adding other capability through passed-in variables. Drove me up the wall.
I finally had the need to fix it for real, and after a few hours found this post:
https://laracasts.com/discuss/channels/laravel/laravel-v5415-on-heroku-all-routes-but-not-working
TL/DR; I had duplicated a (correctly working) heroku repo to create my new one, and somewhere along the way my web/.htaccess file got deleted. I replaced it, and everything works like a charm.
For me the provided test also worked well (I'm using PHP): if eaxmple.com/ROUTE doesn't work, but example.com/index.php/ROUTE does work... you have an .htaccess problem.
Also, for reference, here's my entire .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
Hopefully this saves someone some searching...

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');
}
}

Converting a rewrite from htaccess to nginx

I'm currently using this htaccess code in Apache to change the URL in address bar from http://www.domain.com/list?m=100 to http://www.domain.com/list/100
RewriteEngine On
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]*)$ index.php?m=$1 [QSA,L]
</IfModule>
The above htaccess is on /list directory.
I have tried to convert it to nginx (using http://winginx.com/en/htaccess), but I couldn't make it work. This is what I tried
location /list/ {
if (!-e $request_filename){
rewrite ^/([^.]*)$ /index.php?m=$1 break;
}
}
The above just downloads the PHP code. I also tried changing the break to last, but it just opens the homepage (the url in the address bar changes to http://www.domain.com/list/100). Any suggestions how I can make it work?
You don't need to convert everything, use this:
location /list/ {
rewrite ^/list/(.*)$ /index.php?m=$1 last;
}
Don't forget to restart nginx afterwards to see the changes.

.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.

Slim Framework always return void 200OK

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

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