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:
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:
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:
Lastly we match the opening tag with a closing tag, telling Apache that this is the end of the block:
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:
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:
... 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.