1

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.

Thanos7878
  • 13
  • 3
  • 1
    The php server does not read .htaccess files. Is this a problem with you slim routes (if you replace your index.php with “ – AD7six Mar 20 '22 at 17:48
  • No there isn't a name.ex folder, i don't want to express a folder with the name.ex i want to know if can pass name.ex as an argument, because right now it returns an error. If u see the two examples i mentioned, in the first one the argument "name" passes correctly but the "name.ex" is not. – Thanos7878 Mar 20 '22 at 18:10
  • when i type the http://localhost:8080/name.ex/oth, the slim output is "GET /name.ex/oth - No such file or directory". So from what I understand when there is a dot (.) in the request url php/slim automatically detects it as a file. Is there a way to bypass that? Its not a run error as its already working correctly if there are no dots in the request url. – Thanos7878 Mar 20 '22 at 19:19

1 Answers1

2

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.

Not Found, The requested resource /blah.ex/blah was not found on this server

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...
AD7six
  • 63,116
  • 12
  • 91
  • 123
  • That was it, I made a router file and everything works fine. Thank you! – Thanos7878 Mar 20 '22 at 22:33
  • `I made a router file` Glad the problem is solved but I'm not actually sure what you've done, the answer does not involve changing any files - only the arguments passed to `php -S` – AD7six Mar 22 '22 at 15:03
  • I have the same problem with the dot on the URL and I solve applying the solution of start the PHP dev webserver like `php -S localhost:7004 -t / index.php`, having the index.php on the root project. That's when I can access to urls with dots. But,, at this moment, the files with relatives direction, like images or css files become inaccessible. How to solve that? – zacktagnan Feb 18 '23 at 17:20
  • @zacktagnan hard to tell from that comment, sounds like a code error requesting the wrong path. Please treat as a new question, i.e. research existing questions/answers and if you need to ask a question. It's fundamental to clarify what url is requested (for these images/css. files), what the response is and why it's different from what you expect. – AD7six Feb 19 '23 at 10:43
  • @AD7six ... For the css file, that's the url to requested it: ``, similar to request an image, i.e.,`Icono de App-MíA` ... I, also, try to prepend those urls with the base URL. In the case of dev server, something like http://localhost:8000. So, for the css file: ``. But this solution doesn't work either. – zacktagnan Feb 20 '23 at 11:35
  • @AD7six ... On the other hand, I do have a question asked about it but I do not receive any answer. https://es.stackoverflow.com/questions/583505/permitir-punto-en-la-url – zacktagnan Feb 20 '23 at 11:45
  • @zacktagnan if you want php to handle requests with a `.` in them, you also then need to make php handle requests for files, i.e. [something like this](https://stackoverflow.com/q/3697748/761202) - From the linked question I'm hoping you are not running `php -S` in production ... :). It likely has downvotes because, like here, you haven't clarified what url is being requested that doesn't work and leave it to the reader to figure that out. – AD7six Feb 20 '23 at 14:21
  • @AD7six ... So, I have to handle files, or static files, through PHP. Well. It seems to difficult, but well... "... you are not running php -S in production": no, it's a dev server. Taking advantage of this, I want to ask: this problem of the dot in the URL doesn't cause ERROR on a production server? and it's something that appears on a dev mode?...Finally, you say that I don't put an example of an URL that cause error. Maybe I don't put an specific example What I am doing is giving a generic one and explaining how the ERROR is produced ... http://whatever-domain/users/usernameofuse/profile. – zacktagnan Feb 21 '23 at 18:42
  • But well ... Finally, maybe, it's more simple to limit the format of the username and not accept usernames with a dot. – zacktagnan Feb 21 '23 at 18:43
  • @zacktagnan Please treat as a new question, i.e. research existing questions/answers and if you need to ask a question. – AD7six Feb 21 '23 at 21:01