Using ForceType For Nicer Page URLs - Implementing ForceType sensibly
(Page 3 of 5 )
I'm sure you've seen many sites that have URL's like www.mysite.com/articles/php/scripting and thought to yourself "geez, how do they manage so many directories and files?". On popular sites there's a very good chance that what actually looks like you're viewing pages in a sub-directory of the site in Apache's ForceType directive in action.
It's the beginning of June 2002, which is Soccer World Cup time. Being the soccer nut that I am, I thought that I would inject some soccer flavour into the example for this article. What we’re going to do is use Apache's ForceType directive and some PHP to let people visit our site and see stats about fictional soccer teams and their players. The site will be setup in such a way that a user can learn about a team by entering the following URL into their browser:
http://localhost/soccer/[team name]
For example, if we have a team called the Tapians, then we could enter this URL to learn some quick stats about the Taipans team:
http://localhost/soccer/Taipans
We also want each team to contain the details of multiple players, so we will also make it so that we can use:
http://localhost/soccer/[team name]/[player name]
... to learn some quick stats about a player from a specific team. For example, if the Taipans had a player called Tim Jones, then we could see some quick stats on him by going to the following URL:
http://localhost/soccer/Taipans/Tim Jones
OK, let's get into it. First off we need to create a file called "soccer" with no file extension. This will form the /soccer part of our URL. It will contain PHP code that will grab the details of the requested URI and manipulate it. We will look at the code for this file in a minute.
Next we need to create a file called ".htaccess" and enter the following code into it:
<Files soccer>
ForceType application/x-httpd-php
</Files>The .htaccess file
must exist in the same directory as the soccer file. To give it a quick test, upload both files to your web server and visit your site like this (obviously replacing localhost with your server IP/domain name if it isn't on your local machine):
http://localhost/soccer
You should see an empty page in your browser. If you get a 404 file not found error or any other type of error, then double check your httpd.conf file to make sure that you are allowed to setup directives in a .htaccess file for the directory where the soccer file exists.
As one last test, edit the soccer file and add the following PHP code to it:
<?php
echo "test";
?>Run the soccer file in your browser again and you should see the word "test" in your browser. If this works then Apache is now recognizing the extension-less soccer file as a PHP script. We're now ready to code with PHP to show team/player details depending on the URL of the soccer script.
Before we move on we need to understand one of Apache's best features: URL look back. With Apache, if you enter a URL that doesn't exist, it will work from the end of the URL back to the beginning, trying to match that part of the URL to a file that exists on the web server.
In our example we have a file called soccer with no quotes, so if we entered the following URL in our browser:
http://localhost/soccer/blah/blee
... then Apache would start from the right side of the URL looking for a directory called soccer/blah/blee/. It wouldn't find a directory called soccer/blah/blee, so it would then look for a file called blee in the soccer/blah directory. Obviously this doesn't exist, so Apache steps backwards down the URL and looks for a directory called soccer/blah/. It then continues to unsuccessfully find a file called soccer/blah, and finally it finds a file called soccer, which it calls up, processes, and spits back to the brower.
All of the text in the URL after soccer (/blah/blee) is stored in a server variable called REQUEST_URI. This REQUEST_URI variable is the key to implementing the ForceType directive in combination with a PHP script.
Next: The PHP script >>
More Apache Articles
More By Joe O'Donnell