views:

22

answers:

2

I have some javascript that is generated by PHP. Currently I am including the javeascript in the html using

<script type="text/javascript" src="js/script.php">

But I want to use

<script type="text/javascript" src="js/script.js">

Now script.js does not exist, but I want it to redirect to script.php without the user knowing.

Can this be done with .htaccess?

+3  A: 

If your web server supports mod_rewrite, you could do something like this:

RewriteEngine On

RewriteRule ^js/script\.js$ js/script.php

If you have more than one script, you could generalize that RewriteRule by using a backreference from the test pattern:

RewriteRule ^js/(.*)\.js$ js/$1.php
Tim Stone
A: 

If you just want your script src to end in ".js" you could always leave it as a .php file and link it in like this:

<script type="text/javascript" src="js/script.php?name=script.js">

This technique is commonly used with php or other scripts which generate images:

<img src="images/thumbnailer.php?x=foo.jpg">

This way if the browser tries to determine the type of file by the "extension," it will be "tricked" into using the right format.

no