user3552105 user3552105 - 1 month ago 6
Apache Configuration Question

Node JS Application running on Apache CentOS6 - htaccess

We have searched and searched, but cannot for the life of us figure out how to edit our

.htaccess
file to allow our node.js application to run.


  • Our root domain runs a wordpress website. We have built a custom API in Node to be deployed on
    www.website.com/api
    .

  • We have
    node
    successfully installed on our centOS6. We are also able to run the node server successfully in the terminal by navigating to the
    /api
    folder and running
    nohup node my_app.js &
    . The logs confirm the server is running on port 51900.



Here's the problem. We know the node API is running, we just cant figure out how to see it. We're pretty sure we have to change our
.htaccess
file to redirect the
/api/
directory. Here is a copy/paste of our
.htaccess
file and what we have so far. Our redirects don't seem to work though.

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteRule ^/api$ http://127.0.0.1:51900/ [P,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/api/(.*)$ http://127.0.0.1:51900/$1 [P,L]
RewriteRule . /index.php [L]

</IfModule>

# END WordPress


Any help is greatly appreciated.

Answer

You need to exclude your API URL from wordpress. Also in .htaccess you do not prepend a / in the rewriterule e.g ^/api. There is no prepending slash in directory (htaccess) context. It should be ^api. So that's also why your rule is not matching. Try this and see if it helps.

<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>