3

I want to serve my JavaScript files via a PHP script.

An example would be:

If I were to request a JavaScript file called dir/my_file.js

The server should redirect the request to a handler script called my_script_server.php?src=dir/my_files.js

How can I do this in Apache?

Baumr
  • 103
  • 5
Christian Toma
  • 160
  • 1
  • 8

3 Answers3

2

Something akin to this is probably what you're after:

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(dir/.*\.js)$ my_script_server.php?src=$1 [L,QSA]
</IfModule>

The lines that end -f and -d ensure that the file isn't already there, and the final rule looks for requests that match .js files in the folder dir/ and transparently sends them to your php script.

I've modified that code from an htaccess file I had lying around, but it would be similar if you were to put it into the httpd config file I believe.

There's way more info in Apache's docs on the mod_rewrite module.

PeterJCLaw
  • 101
  • 1
2

You could just serve the script from a php file - this works fine from ASP so should be equally fine for PHP.

I usually keep ".js" in the file name so I know what is what at a glance, such as "some_dynamic_script.js.asp" or in your case "some_dynamic_script.js.php". Then just make sure your script outputs a correctly formatted javascript response and include it in the usual way with soemthing like <script type="text/javascript" src="some_dynamic_script.js.php"> - you can even pass options on the querystring for the php code to react to if desired, like <script type="text/javascript" src="some_dynamic_script.js.php?option1=value1&option2=value2&option3=value3">.

If you are likely to distribute your code to others or install it on systems you don't control this has an advantage over mod_rewrite in that you don't have the extra installation step (adding the rewrite controls to the apache config or .htaccess, which might not work if the host does not have mod_rewrite enabled).

I recall there were problems with some older browsers not liking javascipt being served without the right mime type, so make sure you set this with header("Content-Type: text/javascript"); just in case (otherwise the response will probably be server as text/html by default) - though I've just noticed some of my code isn't doing this and it is working fine in IE6/7/8, FF2/3/3.5/3.6 and at least one version of Chrome.

One major issue to be aware of, whether you use mod_rewrite or php more directly, is of course that if the php code does anything with input from the query-string or the app's database you need to ensure, like all your code, that said data is properly screened to avoid potential injection and XSS attacks.

Another, more minor, issue to be aware of is cache control. The PHP processor will default to marking the output as always needing to be re-requested, where for a plain JS file Apache may return a "304 not modified" response if the browser indicates it has previously made the same request, which if the script being returned does not change could be a waste of your bandwidth and make the response seem slower to remote users. This is a non-issue if your app is always hosted on HTTPS (as nothing served via HTTPS should be cached anyway) but might be a consideration for large blocks of script severed over HTTP. If this could be a problem for you there are ways you can manipulate cache control to regain its advantage.

David Spillett
  • 22,754
  • 45
  • 67
0

Use a combination of things:

  • mod-rewrite to make the php appear to be a js file
  • send a header from the top of the php file to make the browser think it's really javascript

    header('Content-Type: application/javascript');

The really nice part is you can add dynamic content to the 'javascript file' from the server, like this for example, passing the environment from php to javascript:

var environment = <?=getenv('APPLICATION_ENV');?>

// More JS code here ...
quickshiftin
  • 2,125
  • 5
  • 27
  • 41