-
-
Save iPublicis/c5e8ae1fe3f516723c2a902830603afa to your computer and use it in GitHub Desktop.
######################### | |
# | |
# NODE.JS app running in Apache | |
# sample .htaccess - partialy based on vielhuber/.htaccess | |
# Read also https://gist.github.com/vielhuber/f2c6bdd1ed9024023fe4 | |
# Also rules to enforce www. prefix and https: SSL access and avoid extra processing for any file defined. | |
# | |
# This file must be on the dir where Apache expects to find the website | |
# The Node App can be anywhere else but must be accessible as explained below. | |
# | |
# This may not work in a shared environment or if you don't have a minimum access to the server environment. | |
# This works in a dedicated or virtual server environment with CPanel or other panel if you can manage server's software. | |
# | |
# ModRewrite must be active on Apache | |
Options +FollowSymLinks | |
RewriteEngine On | |
# First check if domain starts with www. and add it | |
RewriteCond %{HTTP_HOST} !^$ | |
RewriteCond %{HTTP_HOST} !^www\. [NC] | |
# If not using SSL force it | |
RewriteCond %{HTTPS}s ^on(s)| | |
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L] | |
RewriteCond %{HTTPS} off | |
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=302,L,NE] | |
# Check if the request is for an image file (you can add any extension here) | |
# if it is don't do anyhting more because itsn't required to process it with NodeJS | |
RewriteCond %{REQUEST_FILENAME} -f | |
RewriteCond %{REQUEST_FILENAME} \.(jpg|jpeg|png|gif|bmp|webp)$ [NC] | |
RewriteRule ^ - [L] | |
# Redirect all trafic to NodeJS server. | |
# It must be running and, in this case, listening to port 60000 | |
# If it is listening in a different port adapt the code as fit | |
RewriteRule ^$ http://127.0.0.1:60000/ [P,L] | |
RewriteCond %{REQUEST_FILENAME} !-f | |
RewriteCond %{REQUEST_FILENAME} !-d | |
RewriteRule ^(.*)$ http://127.0.0.1:60000/$1 [P,L] |
A lot of confusion on this config, especially in shared hosting situations.
- Why do you need to FORCE all requests to be HTTPS ?
The Node.js application itself can explicitly code all legitimate requests to be HTTPS and WWW.
Any requests not fitting into the HTTPS and WWW schema can therefore be logged as potential hacks.
- You redirect ALL requests to the Node.js HTTP port on the server.
This creates a problem for requests for HTML/CSS/FEJS/IMGS/etc files.
In reality, you should only redirect requests with a defined pseudo-folder, e.g. "mynodeapp", to the Node.js port.
For example, the request to URL
https://www.mysite.com/mynodeapp/fetch-it
would be redirected to
http://127.0.0.1:60000/fetch-it
which is the server's Node.js app server.
But any request for a website page, e.g. the site's home page on URL
https://www.mysite.com
would be directed straight to the website's root directory and the index.html file listed in the URL.
But this code doesn't perform the redirect successfully for me anyway using cPanel on shared hsoting.
I get a 503 error and "Service unavailable" message.
would be directed straight to the website's root directory and the index.html file listed in the URL.
But this code doesn't perform the redirect successfully for me anyway using cPanel on shared hosting. I get a 503 error and "Service unavailable" message.
It works in a dedicated environment where the index page is also served by NodeJS. I'm not using this in a shared environment with CPanel.
You can easily add exclusions for specific files as you want. It's Free Software. Fork it and adapt it as you please to serve your purposes.
Yes, shared hosting doesn't usually allow Node.js apps - for commercial reasons more than tech ones: shared hosting providers usually have deals with cPanel and cPanel is an official partner of WordPress . . .
Nice of you to stop by your GitHub zones once in a while.
Nice of you to stop by your GitHub zones once in a while.
Thanks. Yes. I'm neglecting this a bit hence I'm now more an Agile Coach and not as much a programmer as before.
I'm going to add a note that this may not work in some shared environments if you don't have control of what is running there
This creates a problem for requests for HTML/CSS/FEJS/IMGS/etc files. In reality, you should only redirect requests with a defined pseudo-folder, e.g. "mynodeapp", to the Node.js port.
Added another piece of code so it checks if it is an image (you can change as you see fit) and stops processing.
With this, requests to NodeJS server are way less.
thanks