3

I'm working on a project where website content is based on PHP(CodeIgniter) behind Apache server, and my duty is to write a node.js as an API to upcoming mobile version of the app. (Note, our hosting is managed, we do not have access to virtualhost so using proxypass is out of reach)

I have to make my node file run behind Apache, in a URL like "domainname/api". When said URL is called, Apache has to bridge this request to localhost:port, where node is living on.

Here is my node.js file:

var fs = require('fs');
var http = require('http');
var app = require('express')();
var options = {
};

app.get('/', function (req, res) {
       res.send('Hello World!');
});

http.createServer( app).listen(61000, function () {
       console.log('Started!');
});

Here is my .htaccess file:

RewriteEngine on 
RewriteRule ^$ /index.php [L] 
RewriteCond %{HTTP_HOST} !^ourdomainname\.com
RewriteRule (.*) http://ourdomainname.com/$1 [R=301,L]
RewriteCond $1 !^(index\.php|info.php|resource|system|user_guide|bootstrap|robots\.txt|favicon\.ico|google<somevalues>.html) 
RewriteRule ^(.*)$ /index.php/$1 [L]

RewriteRule ^/api$ http://127.0.0.1:61000/ [P,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/api/(.*)$ http://127.0.0.1:61000/$1 [P,L]

Although node works locally(on our managed hosting server) (I can call "lynx localhost:61000/" and see proper output), it doesn't work on outside local. All our calls to "ourdomainname.com:61000/api" is sent to CodeIgniter(PHP), not to our node.js file.

Any help is appreciated.

1 Answer 1

2

I had a similiar issue with running Node.js alongside a wordpress application. Wordpress in the root, and the node app in www.website.com/api. Here is a copy of our .htaccess file. We struggled with this a lot. What finally worked (thanks to a suggestion on stackoverflow) was to move the /api redirect to the top of the file. You also don't need the initial '/' before the directory api.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteRule ^api/(.*)?$ http://127.0.0.1:51900/$1 [P,L]

RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

</IfModule>

Hope this helps!

Sign up to request clarification or add additional context in comments.

1 Comment

It should be noted that you should add RewriteRule ^api/(.*)?$ http://127.0.0.1:51900/$1 [P,L] before WP Section to avoid overwitten rule when there is an update to htaccess by WP

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.