Last active
January 16, 2024 08:04
-
-
Save iPublicis/c5e8ae1fe3f516723c2a902830603afa to your computer and use it in GitHub Desktop.
NODE.JS app in Apache - force www. and https: sample .htaccess - partialy based on vielhuber/.htaccess
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
######################### | |
# | |
# NODE.JS app running in Apache | |
# sample .htaccess - partialy based on vielhuber/.htaccess | |
# Also rules to enforce www. prefix and https: SSL access | |
# | |
# 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. | |
# | |
# ModRewrite must be active on Apache | |
RewriteEngine On | |
# First check if domain starts with www. and add it | |
# then if not using SSL force it | |
RewriteCond %{HTTP_HOST} !^$ | |
RewriteCond %{HTTP_HOST} !^www\. [NC] | |
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] | |
# Redirect all trafic to NODE.JS server. | |
# It must be running and, in this case, listening to port 60000 | |
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] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A lot of confusion on this config, especially in shared hosting situations.
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.
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
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.