Home arrow Apache arrow Page 2 - Using ForceType For Nicer Page URLs
APACHE

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 / 56
June 05, 2002
TABLE OF CONTENTS:
  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
SEARCH DEVARTICLES

Using ForceType For Nicer Page URLs - Apache's ForceType directive
(Page 2 of 5 )

Apache is composed of several modules. These modules allow us to control user authorization, send files to the client, execute CGI scripts, map different parts of the host file system in the document tree, carry-out URL redirection and more. These modules are compiled into Apache when it’s initially configured, and they are all optional.

One module in particular, mod_mime, allows us to determine different things about a document that relate to its content and headers. Mod_mime allows us to use several different directives in httpd.conf and .htaccess. Each of the directives available in the mod_mime module are shown below:
  • AddCharSet: Maps the given filename extensions to the specified content charset.
  • AddEncoding: Maps the given filename extensions to the specified encoding type.
  • AddHandler: Maps the given filenames extension to a specific handler name.
  • AddLanguage: Maps the given filename extension to the specified content language.
  • AddType: Maps the given filename extension onto a specified content type.
  • DefaultLanguage: Tells Apache that all files in the directive’s scope that don’t have an explicit language extension should be considered to be in the specified MIME-lang language.
  • ForceType: Forces all matching files to be served as a specific content type.
  • RemoveEncoding: Removes any encoding associations for files with the given extensions.
  • RemoveHandler: Removes any handler associations for files with given extensions.
  • RemoveType: Removes any MIME type associations for files with the given extensions.
  • SetHandler: Forces all matching files to be parsed through a specific handler.
There are 11 directives in the mod_mime module, however there's one in particular that we will be looking at today. It's the ForceType directive. We're going to learn how, with a little configuration and PHP, we can get rid of "ugly" URL's from our web sites.

The ForceType directive allows us to force a file to be served as a specific content type. For example, PHP files have the MIME type of application/x-httpd-php. Using the ForceType directive it's possible to tell Apache to treat a file with no extension (such as test, myfile, etc) as a PHP file. We can do this with the following code:

<Files myscript>
ForceType application/x-httpd-php
</Files>


The code above tells Apache this: "If someone requests a file called myscript (with no extension) then treat it as a PHP script and forward it to the application setup to handle PHP scripts". Let's dissect what we've just looked at step by step:

As you can see, we use start and end tags to create a "block", telling Apache that we're modifying something to do with one/more files. The <Files> tag combined with the myscript attribute tells Apache that the directives contained within this block should only be applied to the file called myscript:

<Files myscript>

Next we have the ForceType directive, as described earlier. We've already told Apache that we are working with a file called myscript, and this line tells Apache to treat myscript as a file that has the same MIME type as a normal PHP file:

ForceType application/x-httpd-php

Lastly we match the opening tag with a closing tag, telling Apache that this is the end of the block:

</Files>

For the ForceType directive to work as shown above, we need to place the code either inside of Apache's httpd.conf file, or inside a .htaccess file in the same directory where our myscript file exists. Remember that we're setting up Apache so that if we request a specific file with no extension that it will treat it as a PHP file. Instead of creating a file called myscript, we could just as easily have a file called my_php_file or articles or animals:

<Files my_php_file>
ForceType application/x-httpd-php
</Files>

<Files articles>
ForceType application/x-httpd-php
</Files>

<Files animals>
ForceType application/x-httpd-php
</Files>


So let's say that we choose to add the first ForceType directive (for my_php_file) to a .htaccess file in our root directory. From here, we would create a file called "my_php_file" (with no extension) and fill it with PHP code. When we called it up in our web browser with a URL like this:

http://www.mysite.com/my_php_file

... then Apache would treat it as a PHP file and it would be parsed in the same way that any PHP file would.

So what use is a file with no extension? Well it's of great use if we're trying to remove "ugly" URL's from our site and also if we want to have URL's that wont upset the search engines (some search engine don't index pages that have ? or & in their URL, and many dynamic sites have pages such as products.php?productId=10).

On the next page we're going to look at an example that implements Apache's ForceType directive with a bit of PHP to create useful, easy to read URL's.
blog comments powered by Disqus
APACHE ARTICLES

- 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 
Support 

Developer Shed Affiliates

 




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