In part one, we learned how to use PHP and
Apache to provide URLs that look like directories so that search
engines would index them correctly. I also think they just look neat,
much prettier than a bunch of ampersands and equal signs.
Search Engine Friendly URLs
The Intro
I also think they just look neat, much prettier
than a bunch of ampersands and equal signs. This method also results in
shorter URLs since there is no need for the variable names -- just the
values.
I soon discovered, however, that all of the supporting pages lost
their formatting. I quickly determined that this was due to having used
relative URLs in my <link> tags. Further
examination revealed that all of my internal links were broken as well,
and for the same reason. This would have been easily fixed by simply
specifying the absolute URL to the linked files, but I didn't want to
do that because I would have to continually change them every time I
updated the site from my development server. A more elegant solution
was called for.
The Script
<pre>
<?
/* Define our global variables */
global $REQUEST_URI;
global $SCRIPT_NAME;
/* Assign the value of $SCRIPT_NAME to $base_href.
This value is used at the beginning of all links in the site
so that absolute hrefs are created. Relative hrefs will not
work with this method. */
$base_href = $SCRIPT_NAME;
/* Create an array ($path) out of $base_href
so that we can get the name of the current template. */
$path = explode("/",$base_href);
/* Pop the template name at the end of the array and assign it to $template. */
$template = array_pop ($path);
/* Convert $path back to a string to use later in the template.
This variable is used to reference CSS and JavaScript
source files since $base_href includes the template name. */
$path = implode ("/",$path);
/* Extract the values from the end of the URI.
Note that variable names are not included
in the URI, just the values.
It its therefore important that you always
use the same order for your variables
when creating links. The order itself is not
important, so long as it's always the same. */
$vars = str_replace($SCRIPT_NAME, "", $REQUEST_URI);
/* create an array from the string $vars, then
loop over the array, extract each
value, and assign each to a temporary variable */
$array = explode("/",$vars);
$num = count($array); // How many items in the array?
for ($i = 1 ; $i < $num ; $i++) {
$url_array["arg".$i] = $array[$i];
}
/* Since we know what order the values come
in, we can assign each value to it's correct
variable. This part can also be done later in
whichever script needs the info. */
$page = $url_array["arg1"];
$message = $url_array["arg2"];
$message2 = $url_array["arg3"];
?>
</pre>
For the purpose of assigning values to variables, $REQUEST_URI
is great. But to give an absolute path to the current directory --
without hard coding the directory into the script -- it's not enough.
To do that, $SCRIPT_NAME is used, but $SCRIPT_NAME also includes (wait for it) the script's file name. We just want the directory. So, we convert the value of $SCRIPT_NAME
to an array then pop off the last value in the array (the file name)
and convert the array back into a string. Voila! A dynamically
generated path to the current directory.
In the above script, this path value is contained in the variable $path. The $path variable should be used for all internally reference files such as .css and .js files. This will ensure that all of your style sheets and javascripts will work as expected. You can also use the $path variables in your links, but for convenience, I've created a variable $base_href that could be used instead. So, instead of <a href="<? echo "$path/index/someVar/anotherVar/"; ?>">, you would have <a href="<? echo "$base_href/someVar/anotherVar/"; ?>">.
Moving on
The rest of the script is the same as Garrett's, so we'll leave off
further explanation, but there is one more improvement we can make, and
it's to the .htaccess file.
In Part One, Garrett showed us how to use .htaccess to force Apache to
send our script to PHP for processing even though it does not have the
.php file extension. I thought it would be nice to have the document
listed as the default document for the directory so I added the
following line to the .htaccess file:
DirectoryIndex index
This directive tells Apache to use the file "index" as the default document in this directory.
|