router.php on the internal server does not work properly

Asked

Viewed 150 times

0

I created a router.php file in the published root of my project, this file was made as guidance in the PHP documentation for the built-in server. It works as a native change to . htaccess which is not supported by the built-in server.

<?php

if (preg_match('/\.(?:png|jpg|jpeg|gif|js|css|)$/', $_SERVER["REQUEST_URI"])) {
    return false;
} else {
    include __DIR__ . '/index.php';
}

When I use the remote:

php -S 127.0.0.1:8888 public/router.php

to start my server, it does not load CSS and Avascripts showing error:

Not Found

The requested Resource /Assets/css/bootstrap.min.css was not found on this server.

Someone has been through a similar situation?

I’m using php7.1 on Windows but I’ve already tested version 5.6 and the same thing happens.

1 answer

1


First you should run the server inside the public folder avoiding using the public/router.php:

cd public
php -S 127.0.0.1:8888 router.php

Or start with an alternative root Document:

php -S 127.0.0.1:8888 -t public/ public/router.php

Also, when the server is started on a different port this error can happen if the Urls present on the page are absolute and do not include the port in the requested resource address. For example:

http://127.0.0.1/assets/css/bootstrap.min.css

instead of:

http://127.0.0.1:8888/assets/css/bootstrap.min.css
  • Yes, I know that, but the file reference URL is with the port: <link href="http://127.0.0.1:8888/Assets/css/bootstrap.min.css" rel="stylesheet"> And the error even happens by calling the complete file path (which is there) http://127.0.0.1:8888/Assets/css/bootstrap.min.css

  • I updated the answer, tested it here and it seems to be related to the server startup directory.

  • I tried to do that too. It didn’t help

  • It worked. making the command inside the public folder pointing to a hoarse.php file in this directory worked. The only bad thing is that I didn’t want to leave this file in the public folder, but no problem. Thank you!!!

  • Great, you can start the server with a different root directory type: php -S localhost:8888 -t public/ public/route.php

Browser other questions tagged

You are not signed in. Login or sign up in order to post.