简体   繁体   中英

Execute php with node.js | Redirecting with .htaccess

I've got a Node.js-Server:

app.use(express.static(__dirname + '/public'));

The Problem: When I want to execute a php-file like this:

var xhttp= new XMLHttpRequest();

xhttp.onreadystatechange = function(){
    if(xhttp.readyState==4 && xhttp.status==200){

        var text = xhttp.responseText;
        console.log(text);

    }
}

xhttp.open("GET","php/gethighscore.php",true);
xhttp.send();

I'll get the code from the php-file as response:

<?php
echo "test";
?>

I know that Node.js send me the static file but I don't know how to avoid that.

I already tried with the .htaccess-file:

RewriteEngine On

#Redirecting the php
RewriteCond %{REQUEST_FILENAME}       !-f
RewriteCond %{REQUEST_FILENAME}       !-d
RewriteRule ^(.php)$ my-webhost.com/public/php/ [L]

#Redirecting for node
RewriteCond %{REQUEST_FILENAME}       !-f
RewriteCond %{REQUEST_FILENAME}       !-d
RewriteRule ^(.*)$ http://localhost:64332/$1 [P]

But it doesn't work.

When I go to my-webhost.com/public/php/gethighscore.php I get the executed php-script. When I go to my-webhost.com/public/ I'll can use my application without node.js and the php-script is working fine.

I hope you can help me. :)

Try this one replace your my_project name to your project name

RewriteEngine on
RewriteCond $1 !^(index\.php|images|asset|js|robots\.txt)
RewriteRule ^(.*)$ /my_project /index.php/$1 [L]

Ok, I've already got it. I just changed

xhttp.open("GET","php/gethighscore.php",true);

to

xhttp.open("GET","public/php/gethighscore.php",true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM