How to serve Express/node API to subdomain? - node.js

I have a tiny Express app serving on port 35000 on my web server, and it's accessible from mywebsite.com/api using the following .htaccess code (works perfectly, just sends "hello world" to the browser).
RewriteEngine On
RewriteRule ^api/(.*) http://127.0.0.1:35000/$1 [P]
I have set up a subdomain at api.mywebsite.com linked to a separate directory on my server and if I put web files here I can see them at the URL, so that's working fine. I'd like to move my API to api.mywebsite.com so I've created the following .htaccess file in the directory for the subdomain.
RewriteEngine On
RewriteRule ^(.*) http://127.0.0.1:35000/$1 [P]
But navigating to api.mywebsite.com returns a "Not found" error from Express, with a log of 404 index.php. Why is an index.php file expected? I'm happy to at least get the error because that means Express is communicating through the subdomain, but it's not giving me my "hello world" message.
I've had a look at Express vhost from other question but am struggling to understand if that's applicable here or if I'm missing a simple .htaccess rule. Any help would be great thank you.

Just to follow up, this solved my question: htaccess proxy to node app
My .htaccess file in my subdomain directory is now:
DirectoryIndex disabled
RewriteEngine On
RewriteRule ^(.*)$ http://127.0.0.1:35000/$1 [P,L]
RewriteRule ^$ http://127.0.0.1:35000/ [P,L]

Related

Unable to Set HTACCESS to url_rewrite

I have .htaccess in the root of my project but its not re-directing as expected.
My app is trying to call:
http://localhost:8888/api/User //via mamp
But my node backend is running and anything after API may change due to the endpoint:
http://localhost:1377/projname-api/User //via node
I have tried:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^localhost:8888/api
RewriteRule (.*) http://localhost:8888/projname-api/$1 [R=301,L]
But i get a 500 server error in htaccess. I have re-written url's in .htaccess before but can not understand what i'm doing wrong.
Any help would be greatly appreciated.

Is it possible to run node app alongside prestashop instance on the same VM?

I want to run nodeJS app which sole purpose is just to make some redirects based on DB queries, the app resides on main domain let's say example.com.
On a subdomain shop.example.com there is a prestashop 1.7 installed.
The virtual server software is cPanel.
Prestashop instance works as long as I don't install the node app, when I do it seems to make both example.com and shop.example.com unresponsive, and after a while it returns 503. The node app runs on port 8080.
You can try to add a .htaccess file that will redirect requests to the nodejs app. So inside your public_html, create a file named ".htaccess" and put this in it:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^api/(.*)?$ http://127.0.0.1:8080/$1 [P]
</IfModule>
And you should make request to this link instead : http://example.com/api/aS7vXbSDn9
The htaccess will redirect any request which contains /api to the nodejs app and pass whatever comes after the api/ to the app as a url paremeter.
For more information about htaccess, you can refer to this link
You can add these two lines as well (Before RewriteRule):
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
This will make sure you only redirect request made for the api, not requests made to access files or directories.

htaccess redirect without losing url

I want my website to redirect from https://www.sitename.com/single-product/supplier-name/supplier-id/article-name/article-id to https://www.sitename.com/single-product and want the same original URL restored.
Currently, https://www.sitename.com/single-product/supplier-name/supplier-id/article-name/article-id URL gets me to 404
I know I will have to use the P flag in .htaccess file in document root to get this working. But I don't know why my solution is not working
RewriteCond %{REQUEST_URI} ^/single-product
RewriteRule ^(.*)$ /single-product/$1 [P]
I have found a solution not it is not working on my server. I guess mod_proxy is currently off on my server
RewriteEngine on
RewriteRule single-product/* https://www.sitename.com/single-product/ [P]
This is a great website to test these scripts
https://htaccess.madewithlove.be/

htaccess proxy to node app

I am trying to proxy subdomain to node application which is running on port 3000. It proxies requests to files e.g subdomain.domain.com/jquery.js. It works fine and shows file served by node. But when I try to access the root of subdomain then I guess apache tries to find index.php and fails with message Cannot GET /index.php. How can I make it work so it will serve whatever provided by node app?
my htaccess
RewriteEngine On
RewriteRule ^(.*)$ http://127.0.0.1:3000/$1 [P,L]
RewriteRule ^$ http://127.0.0.1:3000/ [P,L]
RewriteEngine On
DirectoryIndex disabled
worked out

redirecting HTTP urls to HTTPS pages

Can someone help me with this?
I manages to redirect already the root access. when someone access my site using sample.com it will redirect in to https://sample.com/ but when someone access the site using this method , sample.com/blog, It still loads the http://sample.com/blog not the https://it should be
The following has worked on most of my projects:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.domain.com/$1 [L]
The following is inside a .htaccess placed in the root HTML folder of the website. Place additional rewrite rules after this rule.
EDIT: this RewriteCond expects HTTP to run on the standard port 80. Adjust it in case you transfer HTTP over some other port.

Resources