'Can I use php-cgi to serve an HTML which refers to CSS/JS files from the parent directory?

tl;dr

My understanding is that php -S localhost:1234 -t some-dir serves the page some-dir/index.html at localhost:1234, expecting that CSS and JavaScript (and other files) that the page requires are in some-dir/., not in some-dir/../..

How can I start a server that serves some-dir/index.html and looks for the required files in some-dir/../..

Long version

I have a directory structure like this:

index.html
css
├── some.css
├── style.css
└── sheets.css
docs
├── index.html
└── a lot of other stuff.html
js
├── some.js
├── javascript.js
└── files.js

And obviously anything referred to from within the main index.html is referred via a path containing no ../.

On the other hand, docs/index.html is automatically generated by KSS, a library for documenting CSS modules, and given it position in the tree, when it refers to the very same files as the main index.html, it does so by prepending ../ to the paths. For instance, if the main index.html has

    <script type="text/javascript" src="js/some-script.js"></script>

docs/index.html has

<script src="../js/some-script.js"></script>

Now, for the purpose of debugging JavaScript code launched upon user interactions with docs/index.html, I start the server like this

php -S localhost:1234 -t docs

because docs is the directory where the index.html is, that I want the server to present; but the problem is that the server looks for the files (referenced by the HTML) in the wrong directory, kind of like it strips off ../, so I get errors like the following:

GET http://localhost:1234/css/main.css net::ERR_ABORTED 404 (Not Found)              section-containers.html:12
GET http://localhost:1234/js/swipeable-container.js net::ERR_ABORTED 404 (Not Found) section-containers.html:412 

How can I serve an index.html which uses scripts and stylesheets (are they all together called assets?) from a parent directory?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source