Home arrow Apache arrow Page 3 - Using ForceType For Nicer Page URLs

Using ForceType For Nicer Page URLs

Apache has features that allow us to setup easy to remember URL's for our web site's pages. In this article Joe shows us how easy it is to do with Apache and a little PHP.

Author Info:
By: Joe O'Donnell
Rating: 5 stars5 stars5 stars5 stars5 stars / 57
June 05, 2002
  1. · Using ForceType For Nicer Page URLs
  2. · Apache's ForceType directive
  3. · Implementing ForceType sensibly
  4. · The PHP script
  5. · Conclusion

print this article

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:


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

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):


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:


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:


... 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.
blog comments powered by Disqus

- Programmatically Manipulating Microsoft Exce...
- Installing PHP under Windows
- Compressing Web Content with mod_gzip and mo...
- Compressing Web Output Using mod_deflate and...
- Setting Up Apache 2.0.45 to Parse PHP Pages
- Custom Error 404 Documents with PHP
- Using Apache and PHP on Mac OS X
- ASP: Active Sessions, Active Logins and Tota...
- Working With Oracle on Windows: Part 1
- The Quick-n-Dirty Guide to Setting Up Apache...
- Installing Apache With SSL: The Complete Gui...
- 7 Powerful .htaccess Customization Tips
- Trap And Get Notified: A Practical Solution ...
- One Way To Use Server Side Includes
- Using ForceType For Nicer Page URLs

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials