homepage Welcome to WebmasterWorld Guest from 54.161.192.130
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
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?
gasell

5+ Year Member



 
Msg#: 4594549 posted 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
</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.

 

phranque

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



 
Msg#: 4594549 posted 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]

gasell

5+ Year Member



 
Msg#: 4594549 posted 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

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4594549 posted 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?

JD_Toims

WebmasterWorld Senior Member Top Contributors Of The Month



 
Msg#: 4594549 posted 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");

phranque

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



 
Msg#: 4594549 posted 3:20 am on Jul 20, 2013 (gmt 0)

welcome to WebmasterWorld, JD_Toims!

JD_Toims

WebmasterWorld Senior Member Top Contributors Of The Month



 
Msg#: 4594549 posted 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