Serve Dynamic, User Specific JavaScript, XML and CSS files Using PHP and Apache

Date Published: 05/03/2009 11:58

The core of server side web development is of course producing dynamic content for your users. Doing this has allowed us to serve to our users exactly what they are looking for, created at run time specifically for them. Doing this with HTML is very common across the world but a technique that not all developers are aware of or (most likely) don't take advantage of is doing this with their other files.

Why would I want to Generate Anything other than HTML at Run Time?

The first and foremost answer is download times. A majority of sites on the web include CSS and JavaScript files which are never used by certain users. Files such as browser specific stylesheets or stylesheets which cover other areas of the site which the user can't access. Reducing these downloads will not only improve the loading time of your site but also lessen the strain on your server. Another possible use for this technique is producing dynamic RSS and Atom feeds. I have covered this in previous articles but on those occasions I used PHP files as my feeds, whereas with this simple technique it is possible for your feed to have the correct file extension.

This technique can be used simply to combine multiple CSS or JavaScript files into one file so that the user does not have to execute as many HTTP requests. The HTTP 1.1 specification states that whilst script elements which which are imported within the HTML, block any further downloads from starting, meaning the more imports you have the longer it takes. Combining your stylesheets and CSS on the server should improve the performance of your website mildly to the majority of users but considerably for those with a slow connection.

I personally use this technique to minify my JavaScript on the fly. This allows me to write a perfectly normal JavaScript file without having to deal with manually minifying it and worrying that my minified version is not up to date. There are dozens of possible uses, its down to you to see which best suit your needs.

How do I do it?

Executing PHP from within other files is as simple as adding one file to your htaccess file. I would recommend creating separate htaccess files for your script folders so that they do not damage any other instances of these file types on your site which do not need to be parsed as PHP. I make this point since the header on an XML file will be parsed as PHP and will error if it is not dealt with correctly.

If you do not already have a htaccess file, or have not used them before create a blank text file named ".htaccess" (note the lack of a file extension) in the folder which contains your scripts. In that text file put the line below which best suits you. You can mix and match these rules all on one line depending on which file extension(s) you are trying to parse as PHP.

#JavaScript Files
AddHandler application/x-httpd-php .js

AddHandler application/x-httpd-php .css

#XML Files
AddHandler application/x-httpd-php .xml

#All Three of the Above on one line
AddHandler application/x-httpd-php .js .css .xml

The lines above will now make your server execute any files matching the extensions defined, within the current and sub directories as a PHP file. You can also use the AddType directive as opposed to the AddHandler but these both have a similar effect, it is generally seen as better practice to use AddHandler.

In order to make sure the users browser can properly understand your new dynamic files make sure you use the header function in PHP to inform the browser of the files mime type. This should go before any output begins to avoid errors.

//JavaScript Files
header("Content-Type: text/javascript");

header("Content-Type: text/css");

//XML Files
header("Content-Type: application/xml");

I have Added the AddHandler Command to my htaccess File but it Does not Work

On some server installations this command is ignored because of other conflicting configuration settings. There is however a work around for this scenario which involves using url rewriting. To do this we first must make PHP files which will act as our dynamic files. Set these files up so they execute exactly how you want your files to do so. We then add a rewrite rule to our htaccess file which masks the fact that our files are really PHP files and let us reference them as whatever file extension we desire.

In this example I have a dynamic CSS file which at the moment is in the css folder and is called style.php. We want to link to this in a web page as style.css in the same folder so it appears to be a regular static stylesheet. To do so we add the following line to our htaccess file.

RewriteRule ^css/style.css /css/style.php

Now when a request for style.css is made the dynamically generated contents of style.php are sent. This is an easy way to achieve the same result but without using the AddHandler command as most server configurations will allow the RewriteRule command.


This is just one a various ways you can use your apache configuration to add value and functionality to your site. If you have any problems with the above or it is not working on your server setup feel free to comment below and I will do my best to help you to get it working.


Sorry comments are currently disabled for maintenence

5 Most Recent Articles

Manually Triggering Events in ASP.NET from JavaScript

A quick guide for ASP.NET developers on how to manually trigger ASP.NET events from JavaScript.

Advanced Use of MySQL Stored Procedures

An article for users of MySQL databases describing how they can use advanced stored procedures to improve efficiently in their applications.

Using MySQL Stored Procedures and Extending MySQLi in PHP

A guide for LAMP developers to using stored procedures in MySQL and extending the MySQLi class.

Reading and Writing to Excel Spreadsheets in Python

An introduction to using the xlwt and xlrd modules for python to interact with Microsoft Excel spreadsheets.

Interact with the Web Using Python and the HTTP Library

This is an introduction to making HTTP requests from a python script/application using httplib.