.htaccess routing to Node App - node.js

I'm using a shared hosting service that always has Apache web server running, so I can't run my Node.js application directly on port 80. Instead, as I've been told by my host, I need to use .htaccess to redirect incoming requests to my Node.js app, which is currently running on port 50000. Here's the .htaccess file they told me to use:
RewriteEngine On
RewriteRule ^$ http://127.0.0.1:50000 [P,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://127.0.0.1:50000/$1 [P,L]
This works well enough, except that when I try to go to mydomain.com, the Node app is seeing a request for /index.php. The tech support for my host seems to be as confused as I am. If I go to mydomain.com/test then Node.js app sees /test, so Apache seems to only be adding index.php on the root URL. Could this be an Apache caching issue from someone accessing the URL prior to the .htaccess file and Node.js app being set up?
UPDATE
At this point, no one seems to have a clue what is going on, so I'm just going to add an 'index.php' route to my Node app. Thanks to everyone who took a look and tried to help out.

You might have DirectoryIndex set up for index.php in apache conf file which may be the reason you are getting index.php automatically, what you can do is to set DirectoryIndex to some filename which may not exist or if it is apache 2.4 use DirectoryIndex disabled in your .htaccess.

This is actually what you are going to want to put in your /public_html directory
In the .htaccess file in the code below you will see
http://127.0.0.1:50000
(50000) is the port you are sending it too. There are 2 spots where you make that update.
Also update the example.com
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R,L]
DirectoryIndex disabled
RewriteEngine On
RewriteRule ^$ http://127.0.0.1:50000 / [P,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://127.0.0.1:50000 /$1 [P,L]
This is a specific configuration for an apache server with nodejs.

Related

.htaccess Wordpress and NodeJS running alongside

i'm trying to add alongside an old Wordpress website a route that servers a NodeJS Instance.
The thing is that I want to run both Wordpress and Node.js on the same server but on different routes.
Wordpress is running here http://example.com
Node is running here http://example.com:61000/oferta-de-pret-traduceri
.htaccess
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^/oferta-de-pret-traduceri$ http://127.0.0.1:61000/ [P,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/oferta-de-pret-traduceri/(.*)$ http://127.0.0.1:61000/$1 [P,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
I don't know Wordpress that well nor do I know .htaccess like I should in this case and I don't want to move everything to NGINX unless I have to.
I want my Node.js instance to run on http://example.com/oferta-de-pret-traducerii not on http://example.com:61000.
Also Wordpress trows a 404 error if I try to access http://example.com/oferta-de-pret-traduceri
Hope someone has encountered this kind of situation and has an easy solution.
Thank you very much in advance !
You might be better off with ProxyPassMatch https://httpd.apache.org/docs/2.4/mod/mod_proxy.html#proxypassmatch
ProxyPassMatch "^/oferta-de-pret-traduceri$" "http://127.0.0.1:61000/$1"
The docs for mod_rewrite say this:
Consider using either ProxyPass or ProxyPassMatch whenever possible in
preference to mod_rewrite.
If you cannot use ProxyPass, try this rule:
RewriteRule ^oferta-de-pret-traduceri http://127.0.0.1:61000$1 [P,L]
I used this tool to test: http://htaccess.mwl.be/
If your node app expects 'oferta-de-pret-traduceri' to be in the URL, you will need this rule:
RewriteRule ^oferta-de-pret-traduceri http://127.0.0.1:61000/oferta-de-pret-traduceri$1 [P,L]

Maintenance redirect Node.js on Apache

I'm currently developing some websites with Node.js, but when I take it offline for maintenance etc. it shows me a 503 (an ugly one...)
It's on a host where I can run Node.js on given port (doh) and I need to place an htaccess file with a redirect to that port in the apache folder.
I've been googling a while, and I can't find it... Help?
.htaccess:
RewriteRule ^$ http://127.0.0.1:*portno*/ [P,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://127.0.0.1:*portno*/$1 [P,L]
It's a shared hosting account with SSH access.
Apache Version 2.2.29
cPanel Version 11.48.0 (build 13)

Redirecting another domain to an existing drupal installation's subdirectory

I've been trying to solve a problem for the past few hours. I am desperate for help!
I have two domains:
rd1.example.com/drupal
rd2.example.com
I have a drupal installation in rd1.example.com/drupal/
Further, I started creating content on drupal in a url alias: rd1.example.com/drupal/rd2/
What I want to do is used the second website to point to that specific content within my drupal installation:
rd2.example.com -> rd1.example.com/drupal/rd2/
I don't want it to be a redirect but an internal hidden redirect. The user will think that they are on rd2.example.com.
I've switched rd2.example.com to point to the directory where drupal is installed. I've been trying to edit the rewriterules on drupal's .htaccess but with no luck. Any help is greatly appreciated. Here is my rules so far:
#Existing settings
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} !^rd2\.example\.com [NC]
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^ index.php [L]
#New rules to accomodate for rd2.example.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^rd2\.example\.com$ [NC]
RewriteCond %{DOCUMENT_ROOT}/rd2%{REQUEST_URI} -f
RewriteRule .* /rd2/$0 [L]
Drupal relies on the server variables to process its request. That means when you internally rewrite to /rd2/$0, drupal doesn't know what to do with that. You'll need to reverse proxy *from the rd2` subdomain instead. So leave drupal the way it is, then on the rd2.example.com document root, add:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^rd2 [NC]
RewriteRule ^(.*)$ http://rd1.example.com/drupal/rd2/$1 [L,P]
This will only work if you have mod_proxy loaded on your server, otherwise nothing will happen. Mod_proxy can only be turned on through your server's config. You could also setup the reverse proxy from the vhost config for the rd2 subdomain:
ProxyPass / http://rd1.example.com/drupal/rd2/
Beside of proxy, you may also consider to use a symbolic link that made by a shell command like so:
ln -s /path/to/rd1 /path/to/drupal/rd2
More details is explained in Multi-site - Sharing the same code base

Redirect all the data served by https to http

So, here is the situation:
We are running a website which is powered by Drupal. Sometime ago, it was decided that the website should be served as SSL. The settings to redirect the site from http to https was done by a guy who is not with us anymore.
I can see in the .htaccess file the following lines
#Redirect http to https
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://docs.dev.domain.com/$1 [R,L]
mydomain.com points to the root of the LAMPP server and my site is in a folder inside the webroot (docs.dev.domain.com/mysite).
Now, it has been decided that the SSL is not needed and it has to be removed and all the pages must be served via http (301 redirect).
When I do that in the .htaccess file by using the RewriteRule to redirect a URL (e.g. https://docs.dev.domain.com/mysite/content/book) to http when a user visits https by using:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^ index.php [L]
#Redirect HTTPS to HTTP
RewriteCond %{SERVER_PORT} ^443$
#RewriteCond %{HTTPS} on
RewriteRule ^(.*)$ http://docs.dev.domain.com/mysite/$1 [R=301,L]
#even tried this - RewriteRule ^(.*)$ http://docs.dev.domain.com/$1 [R=301,L]
but it redirects every request on https to http://mydomain.com/mysite/index.php (even the urls like (https://docs.dev.domain.com/mysite/content/book/1 which should ideally be redirected to its http counterpart).
How can I remove the https so that my dynamic URLs are served via plain http?
Sorry if this is very novice problem.
This is happening because you have a routing rule before the redirect. The rewrite engine will loop until the URI comes back unchanged, and at that point, it evaluates if the URI needs to be passed onto other modules in order to handle redirecting or proxying.
That means your request is getting routed to index.php, then the rewrite engine loops, then it sees that the request (which at this point is simply /index.php) needs to be redirected, so the request gets flagged to be redirected. Then mod_rewite redirects, but by now, the URI is mangled by your routing rule.
You need to swap the order:
RewriteCond %{HTTPS} on
RewriteRule ^(.*)$ http://docs.dev.domain.com/mysite/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^ index.php [L]
Enabling SSL or disabling it in Drupal has very easy method by enabling/disabling a module. You should check the module name securepages & if it is there you just need to disable it for disabling HTTPS...
Then you can go to your YOURSITE/sites/default & look into settings.php & if $base_url is defined then just remove the 'S' from 'HTTPS'.
You are done...
I don't think that guy enabled it from .htaccess & so I gave my answer even after you solved it by Jon Lin answer..

Changing the working directory using .htaccess

I run TYPO3 with a normal site and a subdomain in the same TYPO3 installation. On the normal site, automatically a mobile device is detected and then rerouted to the subdomain.
In my subdomain the visitor is redirected to the correct page in the TYPO3 tree, using following .htaccess file.
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{http_host} ^m.mysite.com [NC]
RewriteRule ^(.*)$ http://m.mysite.com/index.php?id=74 [L]
What happens is that the visitor ends up in an endless loop where he is sent to the TYPO3 site and then to the subdomain and back again.
To get this working, I also need to change the working directory to www.mysite.com (I also have another provider where I can make these changes using a cpanel, and that site runs perfectly). I have been looking everywhere and have tried almost anything, but I simply do not have enough knowledge to change the working directory.
So what code should I use to change the working directory?
Use RewriteCond to not follow this rule for index.php.
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{http_host} ^m.mysite.com [NC]
# Failing the index.php check, the usual test is to look for any
# existing file
# EDIT: Changed URI to start with / - should work per comments
RewriteCond %{REQUEST_URI} !^/index.php
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://m.mysite.com/index.php?id=74 [L]

Resources