ID:102241
 
Keywords: php, programming
A long, long time ago, I made mentioned a new method I adopted for generating clean URLs and doing page redirects on the fly. It essentially boils down to redirecting all non-existing files/directories to the index.php and parsing the URI within PHP itself to load the desired page.

I still use this method today, and throughout all this time, it has done is purpose almost perfectly (presently it's live on tiberath.com and has been so for at least two iterations of the site). Once I manage to hash out the one problem left, it'll be perfect for what I want to achieve with it.

To re-demonstrate how it works, the htaccess rule I'm using is so:
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

Thanks to intensive google searching, mind you.

The PHP I'm using to parse all of the URI data into the exact method I use for GET and POST data is:
    function fix_uri($nockey = false) {
$uri = preg_split('![/|?|&|$]!', urldecode($_SERVER['REQUEST_URI']));
$i = 97;
$new_uri = array();
foreach($uri as $key) {
if(substr_count($key, "=")) {
$key = explode("=", $key);
$new_uri[ckey($key[0])] = $nockey ? $key[1] : ckey($key[1]);
} else if($key) {
$new_uri[chr($i)] = $nockey ? $key : ckey($key);
$i++;
}
}
return $new_uri;
}

(By exact, I mean exactly that. All GET and POST data I use is done in the form of a=&b=&c=. Although that function takes into account if I want to break away from that method and assign an actual value (such as &id=) to use. But this side is really immaterial to my request.)

The problem is, however, if I want to use this same trick within a subdirectory, the rule will assume the non-existent URI within the directory should be redirected to the root index.php file and not the index.php file on top of the file tree.

For example, if /admin doesn't exist, the rule will be:
tibbius.com/index.php?a=admin

If /admin does exist, it'll be tibbius.com/admin which will then on it's own load tibbius.com/admin/index.php.

If I want to load a non-existent subdirectory from that file, what I'd like is:
tibbius.com/admin/index.php?a=subdirectory

What I actually get is:
tibbius.com/index.php?a=admin&b=subdirectory

If that makes any sense to anyone (I'm hoping it makes sense to the people this post targets).

I can make this all go away if I put that same snippet inside it's own .htaccess file within the /admin directory. And although that's fine in the short-term, at the end of the day, I don't want a .htaccess for every individual subdirectory I create. As forgetting to upload it or copy it around will just infuriate me, and there's that whole "put all of that stuff into the apache config file for that specific site" thing because "excessive .htaccess usage can slow down page load times" etc.

It is 2am, so I may not be as clear in my desire as I can be. If there's still confusion, I'll clarify what I'm doing with examples and better explanation tomorrow.

Thanks in advance.
You may want to try http://serverfault.com/
Branching outside into new websites was the plan if I couldn't get a response here.

But there's no lack of apache2 and PHP wise guys 'round these parts, so I was hoping to converse with one of them before jumping into the lions den.
Warning: file_put_contents(/home/tiberath/tiberath.com/etc/smarty/ cache/browscap.ini) [function.file-put-contents]: failed to open stream: Permission denied in /usr/local/lib/php/browscap/browscap.php on line 455

Warning: touch() [function.touch]: Utime failed: Operation not permitted in /usr/local/lib/php/browscap/browscap.php on line 232

Fatal error: Uncaught exception 'Browscap_Exception' with message 'Could not write .ini content to /home/tiberath/tiberath.com/etc/smarty/cache/browscap.ini' in /usr/local/lib/php/browscap/browscap.php:456 Stack trace: #0 /usr/local/lib/php/browscap/browscap.php(306): Browscap->_getRemoteIniFile('http://browsers...', '/home/tiberath/...') #1 /usr/local/lib/php/browscap/browscap.php(228): Browscap->updateCache() #2 /home/tiberath/tiberath.com/includes/header.php(16): Browscap->getBrowser() #3 /home/tiberath/tiberath.com/index.php(2): include('/home/tiberath/...') #4 {main} thrown in /usr/local/lib/php/browscap/browscap.php on line 456
Thanks, fixed now.
"If I want to load a non-existent subdirectory from that file, what I'd like is:
tibbius.com/admin/index.php?a=subdirectory

What I actually get is:
tibbius.com/index.php?a=admin&b=subdirectory"


based on that, it sounds to me like you need to mod the PHP to know where it is at *during* the URI processing, via something like: http://php.net/manual/en/function.getcwd.php

this will allow you to have your uri parser to automatically include the current directory ("admin/" in your example above) when searching or redirecting.

(yes it's late, but i've been busy)
digitalmouse wrote:
based on that, it sounds to me like you need to mod the PHP to know where it is at *during* the URI processing, via something like: http://php.net/manual/en/function.getcwd.php

this will allow you to have your uri parser to automatically include the current directory ("admin/" in your example above) when searching or redirecting.

Thanks, I'll have a look at it tonight when I get home.

(yes it's late, but i've been busy)

(Haven't we all? ;))