Working around .htaccess, compelte control of the URL.
I found my every attempt in doing it using .htaccess to result in limited usability often breaking with unexpected input. Experiments on tiberath.com resulted in massive headaches until I eventually gave up on it.
So I gots to thinking of how to do it another way. With a little conferring with Mobius, we decided that PHP is a fairly strong language, and we should have no troubles parsing the URL with it.
Unfortunately for us, this sacrifices a little bit of speed (negligible amounts really, but efficiency nuts will yell at us. Eh, they know exactly where they can go. Given today's computers, the milliseconds it takes to run the following functions aren't worth sacrificing the new freedom I gave myself) but makes up everything in usability.
So I got to work, and with a little regex help from Mobius, came up with a solution I was happy with. Of course, anyone who isn't me will probably find the scripts useless, as I format my URL's far different than most people would normally do.
For the most part, you'll find most URLs formatted something not unlike .com?action=account&page=register where I'm more inclined to run through the alphabet: .com?a=account&b=register only varying from this method with important things like an id number: .com?a=members&b=profile&id=20
With this in mind, I wrote the following PHP script.
function fix_uri($uri, $nockey = false) {
$new_uri = array();
$i = 97;
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;
}
function ckey($t) {
return strtolower(preg_replace('/[^a-zA-Z0-9@]/','',$t));
}
|
A quick Google search for the .htaccess code I needed resorted in this .htaccess script:
RewriteEngine on
#Auto-redirect all pages to index.php
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
|
Effectively, all URL's are redirected to index.php. With exception to files and sub-folders. Which was a major bonus I never actually took into consideration when designing this script (and something that had pained me terrible when attempting it with .htaccess redirects).
Throwing this script in my header.php (hearder.php is called before any output to the website is done, and generally only outputs stuff that are on all pages at the very bottom of the file):
$uri = fix_uri(preg_split('![/|?|&|;|$]!', urldecode($_SERVER['REQUEST_URI']))); $uri_a = fix_uri(preg_split('![/|?|&|;|$]!', urldecode($_SERVER['REQUEST_URI'])), true); |
I'm given a clean URL and a complete URL. The resulting executions give me all kinds of crazy URLs to work with, effectively, something like this:
Of course, a URL would never get that big and ridiculous, but you get the idea behind it. Using my own style of formatting URLs (which is in fact, quite handy for say, me, because I don't actually have to change anything in pre-existing websites (minus tiberath.com, that I did differently)), I'm able to provide incredibly clean URLs at the same time as giving myself absolutely no headaches. And if I was to say update the page and give it another division in the URL, I'd have to change absolutely nothing.
The complete index.php you saw on the link above is simply this:
<?
function fix_uri($uri, $nockey = false) {
$new_uri = array();
$i = 97;
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;
}
function ckey($t) {
return strtolower(preg_replace('/[^a-zA-Z0-9@]/','',$t));
}
$uri = fix_uri(preg_split('![/|?|&|;|$]!', urldecode($_SERVER['REQUEST_URI'])));
$uri_a = fix_uri(preg_split('![/|?|&|;|$]!', urldecode($_SERVER['REQUEST_URI'])), true);
echo("The complete URL is: " . $_SERVER['REQUEST_URI'] . "<br /><br />The clean URL is: <pre>" . print_r($uri, true) . "</pre><br /><br />The direct URL is: <pre>" . print_r($uri_a, true) . "</pre>");
?>
|
I'm contented with this. The major setback to it I can foresee but haven't yet tested, is it might mess with Search Engine Optimisation (as technically, all pages will report as index.php). Again, I'm not particularly phased. According to my Google Analytics page, the URLs are reporting correctly, so it's possible Search Engine Optimisation will be saved (or improved if you didn't have any kind of clean URL before hand).
That's all there is to it really. Anyone who uses PHP and hates the thought of .htaccess is free to use it and do as they see fit.
[Update]
The results of the now offline example URL.
The complete URL is: /omg/this/is/a/valid/url?that=seems&to=enjoy?working=properly/using/all&kinds=of?weird=methos;that;I;seem=to/enjoy The clean URL is: Array ( [a] => omg [b] => this [c] => is [d] => a [e] => valid [f] => url [that] => seems [to] => enjoy [working] => properly [g] => using [h] => all [kinds] => of [weird] => methos [i] => that [j] => i [seem] => to [k] => enjoy ) The direct URL is: Array ( [a] => omg [b] => this [c] => is [d] => a [e] => valid [f] => url [that] => seems [to] => enjoy [working] => properly [g] => using [h] => all [kinds] => of [weird] => methos [i] => that [j] => I [seem] => to [k] => enjoy ) |
Posted by Tiberath on Saturday, November 07, 2009 07:21AM
- 8 comments
(link)
/
Keywords:
development
(Edited on Sunday, November 08, 2009 11:55AM)
« Drink Pepsi, annihilate thousands! · You know what I want? »






Login to post a comment.
#8 Tiberath:
Airjoe wrote:
> I still don't understand why you're not using mod_rewrite over PHP.
That was explained in the first or second paragraph. The process of using mod_rewrite was painful and far more limited than what I'm doing now.
Sunday, November 08, 2009 10:46AM
#7 Airjoe:
I still don't understand why you're not using mod_rewrite over PHP.
Sunday, November 08, 2009 10:45AM
#6 Tiberath:
Kuraudo wrote:
> > Throwing this script in my header.php (hearder.php
>
> You also made a typo on "header.php"!
Verm covered that one.
> As for the script, it sounds neat. I take it that actually creating sub-directories was not something you wanted to do? For "url.com/articles/something" you could just create an "articles" directory and in it put a "something" one, stick an index.php in "[www_root]/articles/something/" and be well off enough.
That's pure insanity that. You'll find very little (if any) websites will resort to making subdirectories for things like this, especially when scripts like the above and other redirect methods are already available.
Take BYOND for example.
/members
/developer
/help
/hub
/games
And then they have 'subdirectories' of their own:
/members/tiberath
/developer/tiberath
/help/forum
/hub/tiberath.interfacecontrol
/games/tiberath.isorath
There's no way a folder for each of those exists. That's definitely URL trickery.
Saturday, November 07, 2009 04:30PM
#5 Kuraudo:
> Throwing this script in my header.php (hearder.php
You also made a typo on "header.php"!
As for the script, it sounds neat. I take it that actually creating sub-directories was not something you wanted to do? For "url.com/articles/something" you could just create an "articles" directory and in it put a "something" one, stick an index.php in "[www_root]/articles/something/" and be well off enough.
Saturday, November 07, 2009 10:34AM
#4 Tiberath:
I also said "terrible" instead of "terribly".
And I can appreciate corrections that aren't made just to catch me out. Haywire has this thing about him that he absolutely must call every mistake I make AND do it in a pretentious manor,
(I used a comma instead of a period!)
Saturday, November 07, 2009 08:18AM
#3 Vermolius:
I was going to leave a serious comment, but I'm not, just to irritate you. Instead, I'd like to inform you that you spelled header.php wrong.
Saturday, November 07, 2009 08:17AM
#2 Tiberath:
Oh wow, a typo in the heading. Fascinating. Completely disregarding the entire post mind you, but hey, who cares about the post content?
I'm not going to fix it, just to irritate you.
Saturday, November 07, 2009 07:51AM
#1 Haywire:
Title: complete*
Saturday, November 07, 2009 07:42AM