Welcome to WebmasterWorld Guest from 54.145.235.23

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Executing js file as php?

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

Junior Member

10+ Year Member

joined:Dec 11, 2004
posts: 54
votes: 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
</Files>

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 July 19, 2013 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:10684
votes: 33


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 July 19, 2013 (gmt 0)

Junior Member

10+ Year Member

joined:Dec 11, 2004
posts: 54
votes: 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 July 19, 2013 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:13445
votes: 390


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 July 20, 2013 (gmt 0)

Senior Member

WebmasterWorld Senior Member Top Contributors Of The Month

joined:July 19, 2013
posts:1097
votes: 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 July 20, 2013 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:10684
votes: 33


welcome to WebmasterWorld, JD_Toims!
3:24 am on July 20, 2013 (gmt 0)

Senior Member

WebmasterWorld Senior Member Top Contributors Of The Month

joined:July 19, 2013
posts:1097
votes: 0


Thanks phranque!
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members