I have a customer that sells a multi-tenant SaaS platform. I often have to run the thing locally, but there is some interaction with other websites that then redirect to the customer’s system. And these website won’t redirect to localhost of course. What’s more, the application running on my localhost is running on port 3000, instead of 443 (the default HTTPS port). Let’s see how we can fix this.

The hosts File

First, we need to tell our system that our URL (https://example.com) is actually hosted on localhost. Assuming you’re working on Windows, open the hosts file as administrator. You can find it in C:\Windows\System32\drivers\etc. Then add a line like this:

127.11.11.11     example.com

It doesn’t have to be 127.0.0.1.

When you request https://example.com now, your local system will want to respond. Anything listening on port 443 will now respond. My local application was listening on port 3000, so I didn’t get a response.

The netsh Tool

Open a command prompt as administrator and tell your system to forward requests on port 443 to port 3000. At least, for the correct IP address:

netsh interface portproxy add v4tov4 listenport=443 listenaddress=127.11.11.11 connectport=3000 connectaddress=127.0.0.1

So all requests that enter your machine for 127.11.11.11:443 are sent to 127.0.0.1:3000, which is where my local application was running.

Cleanup

Of course, once you’re done testing, you want to clean everything up. The hosts file is easy: just remove the line.

For the netsh tool, run the following command:

netsh interface portproxy delete v4tov4 listenport=443 listenaddress=127.11.11.11

And now everything’s cleaned up.

So that’s how you can intercept calls and route them to your own application, regardless of which port it is listening to.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

This site uses Akismet to reduce spam. Learn how your comment data is processed.