.htaccess RewriteRule to webserver without changing URL - .htaccess

I would like to do a port-forwarding with .htaccess, like the following scenario:
https://example.com => http://getcontent.from:8080
https://example.com/test => http://getcontent.from:8080/test
https://example.com/favicon.ico => http://getcontent.from:8080/favicon.ico
https://example.com/img/asdf.png => http://getcontent.from:8080/img/asdf.png
But the url should always stay 'example.com'.
The primary target is to forward to a node-server on a different server because I have no https there.

You can use the example #2 from brontobytes, just change the port as well.

Related

Getting Internal Server Error (500) with Node.js setup to use HTTPS

I am using a very simple server script
const https = require('https');
const fs = require('fs');
var options = options = {
key: fs.readFileSync(__dirname+'/certs/private_key.pem'),
cert: fs.readFileSync(__dirname+'/certs/my_cert.crt'),
ca: fs.readFileSync(__dirname+'/certs/my_bundle.ca-bundle'),
}
https.createServer(options, (req, res) => {
res.writeHead(200);
res.end('hello world\n');
}).listen(3000,'example.com');
This example was taken from https://helpdesk.ssls.com/hc/en-us/articles/115001603071-How-to-install-a-certificate-on-Node-js.
My .htaccess looks like this
RewriteEngine on
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com:3000/$1 [L,P,QSA]
The goal is to have all traffic redirect to the server using HTTPS. When I execute the command
node server.js
There are no errors and the server is listening. When I try to connect via a browser using
https://example.com
I get an internal server error and I cannot figure out what the problem might be because I cannot figure out how to get any logs from node.js.
If I setup the server to listen over regular HTTP and set the .htaccess rewrite rule to redirect to the site over regular HTTP it works fine. I cannot figure out for the life of me what I am doing wrong because when I test the server locally with the certs, it seems to work fine over HTTPS (i.e. https://localhost:3000 even though the browser says the cert is invalid). Any idea what the problem is and anyway to get relevant logs from node.js?
I figured out a solution which seems to work but bear in mind I am a noob.
Just some brief context.
Godaddy is currently hosting my files (I'm on the cheapest plan).
Instead of trying to setup node.js as an HTTPS server, I simply run it as a regular HTTP server. For SSL capabilities, I install the certs (private key, bundle, etc) using GoDaddy's configuration tool in CPanel called "SSL/TLS". When this is done, I can type:
https://example.com
and the connection shows as secure. In order to make this work with the regular node.js HTTP server, I had to revise my rewrite rules in the primary .htaccess file. Namely
RewriteEngine on
RewriteCond %{HTTPS} off # To ensure no redirect loop
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L] # Redirect to the secure site
RewriteRule (.*) http://example.com:3000/$1 [L,P,QSA] # Internally, however, pass all request to the regular HTTP node.js server listening on port 3000
Now I can type any URL in the browser (i.e. www.example.com, example.com, http://example.com, https://example.com) and they all redirect to https://example.com and all the website data is fetched normally over the secure connection according to the browser.

Map domain name to a space on the server

I have a domain, e.g., domain.org and a hosting space on the server that has the address http://server/node/myname.
How I can map domain.org to http://server/node/myname such that when I visit to domain.org/some/path I go to http://server/node/myname/some/path?
The domain service offers me web forwarding with HTTP/301 Forwarding or Cloaking Forwarding but none of them works for me. They simply redirect me to http://server/node/myname when visiting domain.org/some/path.
If you have access to apache config It would be Directory config within your VirtualHost config part. see https://wiki.ubuntuusers.de/Apache/Virtual_Hosts/
You can write redirects within your site like here:
https://en.wikipedia.org/wiki/URL_redirection
Modify your .conf file if you have access to Apache configuration like this...
RewriteEngine on
RewriteRule ^(.*) http://server/node/myname/$1 [P]
This will redirect all traffic for the base URL you set above this in that configuration file.
If you already have a rewrite rule in place for domain.org you can use scripting within those pages to do simple URL redirects by parsing the URL on domain.org into a new string for http://server/node/.

HTTPS to HTTP redirect not working HTTPS loading different websites data

I've got a strange problem with https not redirecting to http. I need https://indudlgeinbrighton.co.uk to redirect to http://indulgeinbrighton.co.uk It's on a shared server and I'm getting a strange cross over with the https. When https is put in, it's loading information for a completely different website hosted on the server.
https://indulgeinbrighton.co.uk is the URL affected. I don't normally share the URLs I'm working on, but in the case I think it might be helpful to see what's going on. Where it's showing data for a website called Churchill, obviously this is a bit of a problem. I've tried putting in all the solutions provided on this page How do you redirect HTTPS to HTTP? into the root .htaccess but it's not working. The htaccess is working fine, because when I switched it to load the other way from http to https it worked fine.
I can only assume it's reading the htaccess file from the other site when it's looking at the https version. As I know which site it is, that's easy enough to locate, but what could I put in that htaccess file to redirect this particular url query without affecting https redirects on the other site?
Thank you.
try this in your .htaccess
RewriteEngine On
RewriteCond %{SERVER_PORT} 443
RewriteRule ^(.*)$ http://indulgeinbrighton.co.uk/$1 [R,L]
this is for security measure
The page at 'https://indulgeinbrighton.co.uk/' was loaded over HTTPS,
but requested an insecure script
'http://churchillbrighton.com/wp-content/themes/brighton-hotels/js/avia.js?ver=3'.
This request has been blocked; the content must be served over HTTPS.
you can try to do this in PHP
$protocol = isset($_SERVER["HTTPS"]) ? 'https' : 'http';
or include always external files in https

htaccess: simulating a switch-case on urls especially 'default'

I have a site where I have to rewrite urls with htaccess. Is there a way to handle this like a switch's case statements?
Say my site is foobar.com, and I want
foobar.com/red => foobar.com/republican.php
foobar.com/blue => foobar.com/democrat.php
foobar.com/green/xyz => foobar.com/green.php?id=xyz
default:
foobar.com/* => foobar.com/page.php?query=*
Currently, this is my setup:
RewriteRule ^/red foobar.com/republican.php [works fine]
RewriteRule ^/blue foobar.com/democrat.php [works ok]
RewriteRule ^green/([^/.]+)/?$ green.php?id=$1
[error: redirect loop with foobar.com/green/home.php in browser address because green.php without id set redirects to home.php]
default [no idea how to implement]
Found a way of doing this:
See 'using a route file' on http://kehers.github.io/2014/07/07/url-rewrites-with-apache-and-php.html

Redirect based on port number?

Is it possible to redirect domain.com:88 to domain.com:8080 using htaccess & mod_rewrite?
And example please?
If htaccess can't do it, waht are other options?
If you get requests on different ports you need to have servers listening on those ports. Inside those servers you can certainly configure rewriting rules, you don't even have to check the port, since it is implicitly given inside the servers logic.
Note that you can configure several virtual servers inside an apache http server.
You can also configure a single server to listen on several ports. Check for documentation about the Listen configuration option inside the http server configuration. Then indeed you have to test for the port. You can do that inside the rewriting module by using the RewriteCond command together with the SERVER_PORT variable. So something like:
RewriteEngine on
RewriteCond ${SERVER_PORT} 88
RewriteRule ^(.*)$ http://some.server.addr:8080/$1 [QSA,L]

Resources