54

Assuming that a local Python-Script is running a webserver.

Is there any way to set an alias, so that http://localwebapp/ equals http://localhost:1234/?

Edit: Or at least http://localwebapp:1234/ equals http://localhost:1234/?

devnull
  • 118,548
  • 33
  • 236
  • 227
WhatIsName
  • 2,294
  • 4
  • 24
  • 36

4 Answers4

73

When the browser sees http://localwebapp/ it first tries to determine the IP address of localwebapp. If this succeeds, the browser establishes a TCP connection with that host, using a specific port (which is 80 for HTTP, unless some other port is mentioned in the URL).

Resolving localwebapp to an IP address does not take port information into account, so pointing http://localwebapp/ to http://localhost:1234/ can only be done by means of a HTTP redirection.


To make http://localwebapp:1234/ the same as http://localhost:1234/, edit the hosts file of your operating system by adding the line

127.0.0.1 localwebapp

The location of the hosts file depends on the operating system:

  • For UNIX-like operating systems, it's usually /etc/hosts.
  • For Windows, it's usually: C:\Windows\System32\drivers\etc\hosts
LuckyLuke Skywalker
  • 610
  • 2
  • 5
  • 17
Oswald
  • 31,254
  • 3
  • 43
  • 68
18

the second option (just alias the hostname without the port information) is possible by adding localwebapp to your hostsfile ( /etc/hosts in *NIX, c:\windows\system32\drivers\etc\hosts in windows)

adding

127.0.0.1 localwebapp

should do the trick (assuming your local python script does not do virtual hosting and serves the same content for all domains requested)

Gryphius
  • 75,626
  • 6
  • 48
  • 54
9

You can make localwebapp as alias for localhost in /etc/hosts. Then you can run a webserver (Apache and friends) to detect that hostname.

<VirtualHost *:80>
    ServerName localwebapp

    # redirect elsewhere
    Redirect localhost:1234

</VirtualHost>
Jochen Ritzel
  • 104,512
  • 31
  • 200
  • 194
  • 2
    What to do, if I want to redirect from myfolder.com to localhost/myfolder (just on my private PC). I tried to set VirtualHost with the same way, but it did'nt work for me. Could anybody help? – tomas.teicher Aug 23 '14 at 03:50
1

Just go to this path C:\Windows\System32\drivers\etc. Open the file hosts as administrator and change the localhost configuration. For example in my case I want to show app.com instead of localhost. I just add the app.com in front of 127.0.0.1 localhost and ::1. Host File changes 127.0.0.1 localhost app.com ::1 localhost app.com