homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

Executing js file as php?

 11:58 am on Jul 19, 2013 (gmt 0)

I must be using wrong search terms or something but everything I've tried so far did not work.

The last thing I tried was
<Files "filename.js">
SetHandler application/x-httpd-php

It still returns code as plain text. What is the correct rule to use? I've verified it matches the right file by adding an extra header and this was returned correctly. I'm using Apache 2.2.24 and PHP 5.3.25.



 1:12 pm on Jul 19, 2013 (gmt 0)

this thread might give you some ideas to try - Serving PHP with Apache: AddType and AddHandler:
http://www.webmasterworld.com/apache/4557229.htm [webmasterworld.com]


 3:25 pm on Jul 19, 2013 (gmt 0)

I still didn't find a working version. Instead I opted for an actual php file with js alias like

RewriteRule ^filename\.js$ filename.php


 8:46 pm on Jul 19, 2013 (gmt 0)

It may not have answered your original question, but that's probably a better approach anyway :) If you can't be open and forthright with your own server, when can you be honest?


 12:36 am on Jul 20, 2013 (gmt 0)

I think the .js to .php rewrite is the way I would go too, personally.

A couple of notes...

1.) I'd make sure to use a server relative path to the file, just to make sure the location is always correct and also add an [L] to the rule, which should always be used unless you know exactly why you don't want it.

RewriteRule ^filename\.js$ /filename.php [L]

2.) I'd make sure I set a header so there was no question a browser interpreted the file and executed it as .js rather than something else. (I've run into issues previously with serving images via PHP to set an expires header and if you don't have the headers set correctly by the PHP file so the browser knows it's an image some browsers "choke" and won't display them.)

# At the top of the PHP file
header("Content-type: text/javascript");


 3:20 am on Jul 20, 2013 (gmt 0)

welcome to WebmasterWorld, JD_Toims!


 3:24 am on Jul 20, 2013 (gmt 0)

Thanks phranque!

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved