hi free_light.
The proper way to do it is via URL re-writing in your webserver (I'm assuming Apache here)..
Because your "folders" are virtual (non-existing) as well as dynamic, there's almost no way the web server can respond, other than via a "404 document not found/moved" response.
IF you do not have control of the webserver, but, the web server allows you to have a default 404 page, (e.g. 404.php) which redirects all missing requests to this page, you can code a script to do a header redirect. But I'm guessing here, the chances are slim... The former method is definitely the way to go..
In case you need help to break up the URL into "folder", "file" segments, do check out:
basename(); // extracts the filename of a path/url
dirname(); // extracts the directory name of a path/url
Regards.