3

Is it possible to open a website, for example, http://apple.stackexchange.com with url http://localhost:8000 without changing hosts file?

1

This is quite easy if there's no back story to why you feel hosts file is not the right solution. You also might have problems if you're not used to pasting things in Terminal, but if that's the case - you can ask a second question for help on that too!

Copy and paste the following lines into Terminal:

cd
mkdir diy_proxy
cd diy_proxy
cat > index.html << EOF
<meta http-equiv="refresh" content="0; url=http://apple.stackexchange.com/" />
EOF
python -m SimpleHTTPServer

To kill the python web server, press: Control-C

The lines do the follows:

  1. get you in your home directory/folder
  2. make a folder to hold the index file that will redirect your browser
  3. go to that folder
  4. send characters to the file index.html until you see EOF (and don't include it)
  5. This is the magic that tells your browser to redirect
  6. ends the content to save to file index.html
  7. start a lightweight web server on port 8000 to serve up the index file
  • Also, use care bypassing your firewall and securing the redirect. You don't want your passwords falling out of your pocket, for instance. gnu.gl/blog/Posts/multiple-vulnerabilities-in-pocket – bmike Aug 19 '15 at 0:47
  • The host changed back to apple.stackexchange.com, not what I actually want. – thinker3 Aug 19 '15 at 8:25
  • @metaphy perhaps edit your question to explain more your requirements. It sounds like maybe you want a web proxy? Lots of things will break so you might have to describe the why and what you've tried. – bmike Aug 19 '15 at 9:10
-1

This is possible if you have SSH enabled ("Remote Login" in System Preferences > Sharing)

Then run (in Terminal) ssh localhost -L 8000:apple.stackexchange.com:80

(You get a shell, type exit when finished with the proxy.)

With apple.stackexchange.com it gives me this however:

Error 1003 [...]
Direct IP access not allowed
What happened?
You've requested an IP address that is part of the CloudFlare network. A valid Host header must be supplied to reach the desired website.

To get past that error, you'll need an HTTP level proxy, but ssh might work for some websites.

  • Also a nice solution - simple port forwarding, but as you note, the CDN can get cranky whereas a redirect passes more easily past them. You might need to use something like this golang program to convert the host header from localhost to apple.stackexchange.com stackoverflow.com/questions/30452901/… since CloudFlare now has web socket support in general. – bmike Aug 18 '15 at 23:52
  • 1
    You probably don't need to enable SSH/Remote Login for this. The option in Preferences sets your Mac up for inbound connections, outbound connections should always work. – nohillside Aug 19 '15 at 6:02
  • @patrix: For me at least, the SSH server doesn't appear to run when remote login is disabled. – 0942v8653 Aug 19 '15 at 10:07
  • Ah, right, you still need a server to connect to. – nohillside Aug 19 '15 at 13:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .