Q:

Folder as parameter in PHP

I want to create a script that pass every folder requested in a website as a parameter.
For example, if someone requests:
www.example.com/foo

...that will be redirected to the main index.php and passed as a parameter, getting the same result when requesting www.example.com/index.php?foo
Please note that the folder requested will be random so i can't predict the folder and put a php script there.
Should i handle all the 404 requests through HTACCESS? Or there's a fancier solution?

A:

In your url foo is a parameter key, if you mean to have it like that then what value would it have?
Perhaps your confusing it with a routing structure where you pass the entire url to a routing script that handles the request
The rewrite your looking for is this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?route=$1 [L,QSA]

Then in your index you split the url like this:
<?php 
$parts = isset($_GET['route']) ? explode('/',$_GET['route']) : array();
?>

Then an example URL would look like:
www.example.com/foo/bar/baz
$parts[0] = foo
$parts[1] = bar
$parts[2] = baz

then if your looking for a folders/files existence, something like this: 
<?php 
if (isset($parts[0]) && file_exists('./'.basename($parts[0]))) {
    //Folder or file exists
} else {
    //Folder or file does not exist - 404
}
?>

A:

I think using htaccess is a solution.
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php?$1 [L]

That one will rewrite url not followed by index.php, images or robots.txt to /index.php?{DIR OR VARS}

