Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Relative path in html with php server

I use php -S localhost:8000 as my development server. At the end I'll use nginx. But I have a problem with php server.

root
|
|---/app
    |
    |---/index.html
    |---/scripts
    |   |
    |   |---/main.css
    |
    |---/styles
        |
        |---/main.js

In my app/index.html I have:

<!DOCTYPE html>
<html>
<head>
  <link rel="stylesheet" href="styles/main.css" type="text/css" />
</head>
<body>

  <script src="scripts/main.js"></script>
</body>
</html>

When I open the page in my browser with localhost:8000/app the css and javascript can't be find. The browser is looking for localhost:8000/styles resp. localhost:8000/scripts instead of localhost:8000/app/styles resp. localhost:8000/app/scripts. When I directly open the file withouth server or with nginx, the files are found correctly. So the php server changes something. What's going on here?

I tried it in Chrome and Firefox. Same behavior in both browsers. When I open the website with localhost/app (nginx) everything works as expected. When I open the website with localhost:8000/app (php -S localhost:8000) the scripts and styles can't be found. It's the same files with the same file root. Where comes the different behavior from?

like image 822
Thomas Sablik Avatar asked Mar 07 '26 05:03

Thomas Sablik


1 Answers

After checking the offical docs on PHP: Built-in web server it clearly states:

URI requests are served from the current working directory where PHP was started, unless the -t option is used to specify an explicit document root.

like image 77
icecub Avatar answered Mar 09 '26 23:03

icecub



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!