localhost port mapped to website

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


Solution 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