Skip to content

Instantly share code, notes, and snippets.

@iPublicis
Last active November 4, 2024 00:57
Show Gist options
  • Save iPublicis/c5e8ae1fe3f516723c2a902830603afa to your computer and use it in GitHub Desktop.
Save iPublicis/c5e8ae1fe3f516723c2a902830603afa to your computer and use it in GitHub Desktop.
NODE.JS app in Apache - force www. and https: sample .htaccess, skip images or other files desired - partialy based on vielhuber/.htaccess
#########################
#
# 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]
@corinthian13
Copy link

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.

@iPublicis
Copy link
Author

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

@iPublicis
Copy link
Author

iPublicis commented Nov 4, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment