Add subdomain to localhost URL - http

I am writing an web application that behaves differently depending on a url prefix. The format is something like:
https://myprefix.mycompany.com
The web app behaves differently based on myprefix. My web app extract that part from the URL and act on that.
However, when I test on my local, I use an localhost address:
https://localhost:1234
I counldn't do something like:
https://myprefix.localhost:1234
What is the best way for me to test this scenario?
Many thanks

Unfortunately, because localhost is not a proper domain, you can't add a subdomain to it like that. You can, however, trick your computer into thinking it owns a specific domain and test things that way. For instance, if you have a UNIX-based operating system, open (as root) the file /etc/hosts and add a line (or lines) like this:
127.0.0.1 example.com
127.0.0.1 subdomain.example.com
Your computer will now treat both example.com and subdomain.example.com as belonging to itself. If you visit either in your web browser, they will work the same, in principle, as localhost, but your web server will see the correct domain in its Host header.

I'm not sure about same behaviour on windows. I'm working on linux mint.
You can use lvh.me:port as a local domain. You can imagine that your project is deployed on localhost:port on this domain.
Instead of sub.localhost:port you've to use sub.lvh.me:port
UPDATE
sub.localhost:port works on Chrome.
Note: Firefox automatically adds www. at the beginning of entered domain that can cause problems with subdomains testing

For Windows users, based on this answer and per this comment, you can achieve this by adding ports to localhost via the hosts file that resides at this path:
C:\Windows\System32\drivers\etc\hosts
And append lines like the following to it:
127.0.0.1 example.com
127.0.0.1 subdomain.example.com

One-Line Solution for Windows
Open PowerShell as Administrator and run the following command, replacing sub.mydomain.com with whatever you want.
"`n127.0.0.1 sub.mydomain.com" | Out-File C:\Windows\System32\drivers\etc\hosts -encoding ASCII -append
Breakdown:
`n - newline
127.0.0.1 - loopback address
sub.mydomain.com - domain name
| Out-File C:\Windows\System32\drivers\etc\hosts - pipe the string to the hosts
-encoding ASCII - correct encoding
-append - append to end of file (important!)

You should be using the .test domain for things like that. That is what .test is for. localhost is not supposed to have any subdomains.
To do so violates the approved RFC standards. localhost has an A record and in IPv6 environments, an AAAA record. All other DNS record types, including SOA are forbidden.
Without an SOA record, it cannot be a zone apex that has sub-records, so no subdomains nor delegations are permitted. Even the recent RFC draft titled Let localhost be localhost is consistent with this.

https://myprefix.mycompany.localhost:1234
This should do the trick. Because localhost is a top-level-domain, it behaves like a .com in production code.

It took me a bit of time to find public wildcard DNS domains pointing to localhost so I'm leaving it here for future reference.
The domain that worked for me is localtest.me. That domain and its sub-domains resolve to 127.0.0.1 and ::1.
For example:
$ host localtest.me
localtest.me has address 127.0.0.1
localtest.me has IPv6 address ::1
$ host some-sub-domain.localtest.me
some-sub-domain.localtest.me has address 127.0.0.1
some-sub-domain.localtest.me has IPv6 address ::1
A maintained list of other public wildcard DNS domains that point to localhost can be found in this Gist.

From WSL in Windows:
First navigate to /mnt/c/Windows/System32/drivers/etc(Navigate cause, you may find more interesting files. Don't play here, but see what do they do)
Then do nano hosts(add at very bottom)
127.0.0.1 random.com
127.0.0.1 auth.random.com

Related

forward localhost to ipadress on local netwerk

I have a domain name registered and i made an A record on it that points to 127.0.0.1 (aka localhost). I want to be able to work on websites in this case from machines that are in the same network as the server its running on but those servers are not open to the public internet. I still want to use my test subdomain to access this website but that would point to the machine im working on at that time so it must forward the requests to an 192.168.. ip adress I have set entered on the machine. Is there a windows command i could use that forwards localhost to a specific ipadress, like linux has 'iptables'? Or a program that could do this? Preferably for free.
I dont want to add my internal ipadress to a dns record because that would be sensitive information.
What about adding an entry to /etc/hosts (linux) or c:\Windows\System32\Drivers\etc\hosts (windows) ?
The format is: <ip address> <servernname>, e.g.: 192.168.10.12 www.stackoverflow.com.
Once you did that, accessing http://www.stackoverflow.com from the machine where the hosts file has been edited will end up being served by the server running on 192.168.10.12.

IIS 10 Site Bindings wildcard development machine

I have successfully setup IIS on my local development machine (dev branch - setup as localdev.me) but when I went to setup another branch (hotfix - setup as localhotfix.me) I am running into issues. The issues are due to the way the site is setup. The subdomain of the url is used to determine which Database to connect to. So going to host.localdev.me will connect to the host database. So in IIS I have the following settings for the bindings of the site.
Type Host Name Port IP Address
http localdev.me 80 *
http *.localdev.me 80 *
I can ping localdev.me with any subdomain and I get the loopback address as expected. When I then setup the hotfix branch (exactly the same as the dev but with the following bindings) I get name not resolved errors.
Type Host Name Port IP Address
http localhotfix.me 80 *
http *.localhotfix.me 80 *
Is there a reason the first setup would work and not the second? What is perhaps even stranger if I tell IIS to stop I can still ping subdomains on localdev.me and get the loopback address.
I could always get it working by manually specifying the host name in my windows hosts file but I would rather not do that as I would need to go in and edit the file every time we add a new subdomain.
EDIT: These are the specific errors I am getting.
ping localhotfix.me
Ping request could not find host localhotfix.me. Please check the name and try again.
EDIT2: I have a solution that works fairly well. It requires Acrylic DNS and installation of the Microsoft Loopback Adapter. I set the loopback adapter to a valid IP Address and set the DNS server to 127.0.0.1 then edit the AcrylicHosts file to contain entries for each domain with a wildcard. Once I did all of this I was able to ping localhotfix.me along with *.localhotfix.me. I believe the reason localdev.me worked is because it is a valid domain. The name would resolve at which point I believe IIS was able to take over. But thats really just an educated guess. But kindof makes sense as to why it worked for one and not the other.
The reason *.localdev.me works without a hosts file is because the public DNS for that domain resolves to 127.0.0.1 as long as it is not localdev.me or www.localdev.me. You can check this using nslookup *.localdev.me (replace the asterisk with anything except www) while your hosts file is empty. On the other hand, *.localhotfix.me is not registered in public DNS at all, which is why you'd need a hosts file entry for those.

How to access site through IP address when website is on a shared host?

I want to edit my host file to forward a website to another IP, but that IP is on a shared host, so the IP doesn't take me to the domain I want. Is there a way around this?
i.e.
Website: http://somerandomservice.com/
Ping the site and go to: 67.225.235.59
But they're different sites.
Thanks!
Update: Tried nmap, but unable to find the correct port.
According with the HTTP/1.1 standard, the shared IP hosted site can be accessed by a GET request with the IP as URL and a header of the host.
Here there are two examples(wget and curl):
$ wget --header 'Host:somerandomservice.com' http://67.225.235.59
$ curl --header 'Host:somerandomservice.com' http://67.225.235.59
Resources:
https://en.wikipedia.org/wiki/Shared_web_hosting_service
http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.23
Include the port number with the IP address.
For example:
http://19.18.20.101:5566
where 5566 is the port number.
You can access you website using your IP address and your cPanel username with ~ symbols.
For Example:
http://serverip/~cpusername like as https://xxx.xxx.xx.xx/~mohidul
serverIPaddress/~cpanelusername will only work for cPanel. It will not work for Parallel's Panel.
As long as you have the website created on the shared, VPS or Dedicated, you should be able to always use the following in your host file, which is what your browser will use.
67.225.235.59 somerandomservice.com www.somerandomservice.com

Can a single hosts file entry contain multiple domains/sub-domains?

May be this is a weird question, but I am curious to know if single hosts file entry can contain multiple domains on same line?
I know that following is okay for host entry:
127.0.0.1 somedomain.com
But, I want to know if following would work:
127.0.0.1 somedomain.com, alternate-domain.com, subdomain.domain.com
or
127.0.0.1 somedomain.com, *.domain.com
(Note: I am doing hosts file entries on windows)
May be this has been asked before, but I googled before writing my question, and I didn't quite find a good answer. Thanks for taking time to read and answering, it will be pretty helpful.
I found the solution.
On IIS, I had to add https in bindings associating it to use IIS Development Certificate for SSL in my development environment.
Also, in the process I found that both of the following wiil work for me.
127.0.0.1 somedomain.com, alternate-domain.com, subdomain.domain.com
or
127.0.0.1 somedomain.com alternate-domain.com subdomain.domain.com
Spaces and commas both work, and I can add multiple domains, subdomains combinations on same line.
Also, if you are moving from a non-secure domain to secure domain/subdomain, SSL certificate needs to be setup on web server for it, else it would end up showing an error in the browser.
Cheers!

localhost problem on windows 7

I'm using Windows 7 Pro and want to run my web project locally, but the url www.localhost.com does not work.
If use http://127.0.0.1:2710/default.aspx it works. I checked host file which in the C:\Windows\System32\Drivers\Etc\hosts folder. If I delete the rows:
# 127.0.0.1 localhost
# ::1 localhost
Nothing changes. How can I make www.localhost.com work on my machine?
Hope I understand your question correct. You want to acces your local site using www.localhost.com
Just edit the hosts file and add the following lines
127.0.0.1 www.localhost.com
I checked my Windows 7 hosts file and it has the following lines
# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
Above lines are comments and deleting those lines does not do anything.
Note: If you get "Access denied" when you save the hosts file then open notepad as Administrator and then edit hosts file and save it. It should work
EDIT: The rows you deleted were just comments, so that doesn't matter.
Were you trying to enter your url just as localhost
or as http://localhost:2710/default.aspx?
The latter is probably necessary, as the 2710 is the port number, and without it will default to port 80, which probably doesn't have anything listening on it.
UPDATE: It might actually be an IPv6 thing, so try it with just this line in your hosts file:
127.0.0.1 localhost
You should not have a # at the beginning of the line, that will disable that line.
(You might need to reboot too).
You could also try pinging localhost (just type ping localhost in a command prompt window) and check that you get a reply from 127.0.0.1
Did you try:
http://localhost:2710/default.aspx ?
Okay, simply put, your web project will run on http://localhost:2710/default.aspx, once you put those lines you removed back into the hosts file (More on it here). To get to remove the port number, you need to reconfigure your web server.
Now, if you want to get a domain, that's a different thing altogether... then, you need to get a registrar and use DNS to get it to map to you. You would do this if you wanted other people to be able to use something like www.site.com to get to your site.

Resources