when a request url has a dot in it I am getting a 404 error Not found page that says "The requested resource /error.example was not found on this server." What causes this error and is there a way of fixing it? I am using the Slim framework and run my code with the php built-in server. I will put bellow a sample code that recreates the above error.
index.php:
<?php
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;
require __DIR__ . '/../vendor/autoload.php';
$app = AppFactory::create();
$app->get('/{name}/oth', function (Request $request, Response $response, array $args) {
$name = $args['name'];
$response->getBody()->write($name);
return $response;
});
$app->run();
.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]
Both index.php and .htaccess are located inside the public folder, and i run my code with php -S localhost:8080 -t public/
When I go to my browser and go type the address http://localhost:8080/name/oth it shows name correctly but when i type http://localhost:8080/name.ex/oth it shows the error i mentioned
Not Found
The requested resource /name.ex/oth was not found on this server.
Edit:
Adding the requested information:
-Log for php -S localhost:8080 -t public/:
PHP 8.1.2 Development Server (http://localhost:8080) started
-Log for the request url http://localhost:8080/name/oth:
[::1]:63906 [200]: GET /name/oth
-Log for the request url http://localhost:8080/name.ex/oth:
[::1]:63917 [404]: GET /name.ex/oth - No such file or directory
If i replace index.php with echo 'here'; die; it displays "here" correctly in the browser.
PHP is responsible there, not slim
[::1]:63917 [404]: GET /name.ex/oth - No such file or directory
This log message originates from the php built-in web server, not slim - it's implicit from "No such file or directory" that the request has been considered a file path.
If the response looks like this (php purple :)), i.e. different from anything your own script might generate, that's also strong evidence it's coming from the built-in web server.
This is very likely related to the known behavior of the php dev server considering any url with a . in it as a filepath and not forwarding the request to any php script - see for example this old issue.
Specify a router script
The fix here is simple, specify a "router script" when starting the php development server:
If a PHP file is given on the command line when the web server is started it is treated as a "router" script.
This is also how slim's own documentation say to use the webserver, adapted to the question use:
$ php -S localhost:8080 -t public/ public/index.php
With the server running with an explicit router script, that should resolve the immediate problem of requests not reaching index.php:
$ curl -i http://localhost:8080/blah.ex/blah
HTTP/1.1 200 OK
Host: localhost:8080
Date: Sun, 20 Mar 2022 20:33:58 GMT
Connection: close
X-Powered-By: PHP/8.0.11
Content-type: text/html; charset=UTF-8
...whatever public/index.php returns...
Related
The following code, in index.php, gives the expected routing behaviour when I run it using the built in php web server:
<?php
$request = $_SERVER['REQUEST_URI'];
switch ($request) {
case '/' :
echo 'This is the Home Page';
break;
case '/about' :
echo ' This is the About Page';
break;
default:
echo 'Resource not Found (404)';
break;
}
The same code uploaded to my Ionos Linux web server running Apache does not work: If I access the root directory from a firefox browser (eg http://mywebsites.org.uk/root-directory) I get Resource not Found (404)in the browser. If I append something to the URL (eg http://mywebsites.org.uk/root-directory/about) the browser just gives some page with advertising on which is what you get when there is nothing corresponding to that path on the server). Same happens if you change 'about' for any other word.
I tried with the following .htaccess file in the root directory along with the index.php file:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php [QSA,L]
Now I get the same result with http://mywebsites.org.uk/root-directory. That shows Resource not Found (404) in the browser as before. However, with http://mywebsites.org.uk/root-directory/about or any word substituting 'about' I now get an error message in the browser:
Multiple Choices
The document name you requested (/index.php) could not be found on this server. However, we found documents with names similar to the one you requested.
Available documents:
/index.html (common basename)
When I click on index.html (which is hyperlinked) I find that it is in the directory above the root-directory with the index.php file in it. Ie it is in the mywebsites.org.uk directory. Obviously it is not what I am after.
I also tried replacing the content of the .htaccess file with FallbackResource /index.php. The result was the same as when I had no .htaccess file at all which I described earlier.
I am finding it very difficult to understand my Ionos web server. I do not know if I can change configuration files for Apache or not and I have little understanding of .htaccess files. I have thought of changing to AWS over this issue as the documentation there seems more thorough. However I would prefer not to change if I can sort this issue out. Basically I want to be able to build a PHP router on my Ionos server which runs Apache.,
You are correct that the built-in PHP web server enables you to use a routing script. However, in environments such as your hosting on Ionos, this function is generally performed by whatever web server is running.
Unfortunately, I'm having trouble finding which web server Ionos offers by scanning their web site. Are you certain that they are running Apache and that they have mod_rewrite and local .htaccess overrides enabled? Any of these could explain why you're having issues there.
From your description of your issues, it also sounds like your web site may not have its document root path configured correctly, or that it is not set up to use index.php as a directory index file (e.g. via the DirectoryIndex Apache configuration setting).
The best I can suggest is contacting Ionos techical support or seeking out any technical documentation they offer to customers. I can't even tell if or what configurations they may allow you to override.
I have a multiple GET, POST and PUT routes declared in my Slim v2 App and the API had been working perfectly until today. Out of 2 POST routes, the first one declared is returning a 404 Not Found error. This only happens when I call the API on my GoDaddy hosting; when I test it on my localhost it works fine. This is so weird I have ran out of ideas on how to debug this problem. I am using Postman to execute and test the calls to both of servers, local and GoDaddy. Also, the calls to the GoDaddy hosted API are done via HTTPS. My .htaccess file on both servers looks like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L,QSA]
RewriteRule .* - [env=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
Pastebin to curl calls
Screenshot of Postman call
Pastebin to code fragment
When doing a POST request to /apifolder/v1/addcheckin server responds with 301 redirect to which causes the client to make a GET request to /apifolder/v1/addcheckin. Since there is no such GET route defined server responds with 404 error.
$ curl -i -X POST https://myurl.com/apifolder/v1/addcheckin
HTTP/1.1 301 Moved Permanently
Date: Sun, 26 Feb 2017 02:26:34 GMT
Location: https://myurl.com/apifolder/v1/addcheckin
Check the middleware and the rest of the code to see what causes the 301 redirect.
I am serving a site locally with:
php -S localhost:4444
The url's generated are like: linen-blazer-580.html
So we get the error The requested resource /linen-blazer-580.html was not found on this server.
Normally on apache the .htaccess with mod_rewrite would route that request to index.php but because it has a dot in it, it is trying to find the file which does not exist.
I just got started with Slim. My application for the moment is something like this:
<?php
require 'vendor/autoload.php';
$app = new \Slim\Slim([
'debug' => true
]);
var_dump($app->request());
$app->get('/:name', function ($name) {
echo "Hello, $name";
});
$app->get('/', function () {
echo 'hello world';
});
$app->run();
I am running it on localhost using PHP built in web server. For every request I try in the browser (or Postman, or CURL), what I get is always "hello world", as if the first route is not considered. Moreover, if I remove the second route, I always get a 404.
Am I forgetting something?
For debugging purposes, which HTTP header is used by SLIM to determine the route?
You can't remove the second route $app->get('/') as it is the Home default route and it is quite normal to get a 404 because $app->get('/:name', function ($name) {}) is expecting a callback function's argument 'name' that is missing.
Are you trying the following:
http://localhost/mysite/ --- Outputs Hello World
http://localhost/mysite/marcosh --- Outputs a 404 ??
If this is the case then as a77icus5 suggested we may need to look into your htacess file and what is the project directory structure...
I have a fresh Slim Skeleton install and I thought I'd share my configuration with you...
My Web project directory is as follow :
Webroot
-htaccess
- public
-- htaccess
-- assets
--- js
--- css
- templates
- app
- vendor
-- Slim
-- Twig
In the first .htaccess located in the project root directory I added :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
Here public matches the name of the app public folder
Then in the .htaccess located in the public folder I added :
<IfModule mod_php5.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
</IfModule>
Then in SLIM -> Environment.php (line 142 - Virtual Path ) and try to edit as follow :
// Virtual path
// $env['PATH_INFO'] = substr_replace($requestUri, '', 0, strlen($physicalPath)); // <-- Remove physical path
$env['PATH_INFO'] = str_replace(str_replace('/public', "", dirname($_SERVER['PHP_SELF'])), '', "/".$requestUri); // remove public from URI
$env['PATH_INFO'] = str_replace('?' . $queryString, '', $env['PATH_INFO']); // <-- Remove query string
$env['PATH_INFO'] = '/' . ltrim($env['PATH_INFO'], '/'); // <-- Ensure leading slash
Eventually, I found out that the problem was given by a wrong document root.
I was launching the application from the main folder of my project using php -S localhost:8080 public/index.php and this caused the PATH_INFO header of the HTTP request not to be compiled.
Changing directory to ./public and launching the app using php -S localhost:8080 index.php solved the problem
You need some kind of url rewriting for Slim to work. Since you are using internal PHP webserver you cannot use mod_rewrite. Instead create route.php file to same folder as index.php with following code.
<?php
# Used only for running the app with internal PHP webserver
# php -S localhost:8080 route.php
if (file_exists(__DIR__ . "/" . $_SERVER["REQUEST_URI"])) {
return false;
} else {
include_once "index.php";
}
Then run it with php -S localhost:8080 route.php. Everything works now as expected.
$ curl --include http://localhost:8080/foo
HTTP/1.1 200 OK
Host: localhost:8080
Connection: close
X-Powered-By: PHP/5.6.2
Content-type: text/html;charset=UTF-8
Hello, foo
I am learning to code in PHP and mysql and need to be able to run PHP files using a wampserver. I have correctly installed wamp and have it online through Windows 8, however when I go to run my PHP file I am always given Error 404. I have ensured that my Slim file and .php file are in the correct directory and I have also modified the .htaccess accordingly so that it reroutes to the proper file if a URL is not found since I am testing this on my local machine. Here is a some of my sample code:
require 'C:\wamp\www\easysites\codeguy-Slim-b8181de\Slim\Slim.php';
\Slim\Slim::registerAutoloader();
$api = new \Slim\Slim();
$api->run();
I have traced the error to $api->run() and was getting an error from the call stack at one point that indicated an error had happened in Slim's environment.php at line 123 where the URL is first dealt with. If I run the basic index.php file generated by Netbeans, I get the usual text that alerts me that it is working, however try to run the php file using Slim it errors out which are in the same sub directory under root. Slim is correctly loaded by the php file, but will not allow me to run anything else thus preventing me from seeing the database that I have imported.
Does anyone have an idea of what could be going wrong? I have a co-worker who has successfully run the file but we are unable to make it run on my machine using the same types of changes.
The .htaccess file is as follows:
RewriteEngine ON
RewriteRule ^API(.*)$ /API/api.php [QSA, L]
The file I am trying to run is in a folder contained in the root directory named API, and the file I am trying to run is api.php.
When I followed all the instructions of the installation of slim exactly (installing it on easyphp on my local windows machine), I got the error 404.
I changed the .htaccess to the following, and then it worked fine:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ %{ENV:BASE}index.php [QSA,L]