3

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?

Thomas Sablik
  • 16,127
  • 7
  • 34
  • 62
  • So what are you using? You're saying it works perfectly fine without a server and with nginx. But it doesn't work with "PHP server". PHP is not a server? – icecub Jul 03 '16 at 15:19
  • I use php for development. It's fast but it doesn't work as expected. I want to know, what I have to configure. php -S is a small server for development – Thomas Sablik Jul 03 '16 at 15:23
  • Sounds more like a strange behavior that browser shows... – arkascha Jul 03 '16 at 15:25
  • But this strange behavior comes only with php -S – Thomas Sablik Jul 03 '16 at 15:29
  • 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? – Thomas Sablik Jul 03 '16 at 15:53

2 Answers2

1

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.

icecub
  • 8,615
  • 6
  • 41
  • 70
  • $path contains the path to the file on the filesystem and not the url on the server. This doesn't work. Furthermore index.html shouldn't be parsed. It's a frontend html file without php – Thomas Sablik Jul 03 '16 at 15:49
  • @ThomasSablik Than please don't talk about `index.PHP` in your question in the first place: `In my app/index.php I have:`. If you want to get the server URL instead, all you have to do is: `$server_url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";` – icecub Jul 03 '16 at 15:56
  • I want a relative link using php-webserver. It doesn't work. I don't want to use php-code. `php -S localhost:8000` creates a small development server. But the behavior of this server is not correct. I want to know how to configure this. `./file` or `file` is a relative link to a file. But with php-socket it's changed to an absolute link. I don't want this. – Thomas Sablik Jul 03 '16 at 15:59
  • @ThomasSablik Then I suggest you create a small php file with only this code: ``. It will show you where your `php.ini` file is located which controls the behaviour of your php server. You can change all of its settings in there. – icecub Jul 03 '16 at 16:00
  • I know where the php.ini is. I opened it and I read it. But I didn't find the option responsible for this strange behavior – Thomas Sablik Jul 03 '16 at 16:03
  • @ThomasSablik Check the official docs on it next time please: [PHP: Built-in web server](http://php.net/manual/en/features.commandline.webserver.php). It's like on top of it: **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.** – icecub Jul 03 '16 at 16:15
  • I don't want a different document root. My document root is `root`. That's correct. But every relative link is converted into an absolute link. I want to change it. – Thomas Sablik Jul 03 '16 at 16:20
  • @ThomasSablik You are missing the point. It says "All relative paths are considdered to start from the directory where PHP started". So whether you have a relative path in /root or in /root/app/whatever.. it will ignore that and just start from root. You need to specificly give it the relative path from your root directory even though the file is not. – icecub Jul 03 '16 at 16:23
  • So it is not possible to use relative paths starting from current file? – Thomas Sablik Jul 03 '16 at 16:26
  • @ThomasSablik No it isn't. That's why it's called a development server and not to be used as a real server. It doesn't behave the same way as it is very limited in its capabilities – icecub Jul 03 '16 at 16:27
0

This is an annoying behavior of the PHP built-in web server. Fortunately, there is a workaround: just start your application including index.html in the URI. Open the browser and go to

http://localhost:8000/app/index.html
www.admiraalit.nl
  • 5,768
  • 1
  • 17
  • 32