cPanelResources

Tutorial Editing your local hosts file to preview sites

cPanelResources

Staff
Staff member
Apr 27, 2015
112
74
153
Houston, TX
cPanel Access Level
DataCenter Provider
cPanelResources submitted a new resource:

[Tutorial] Editing your local hosts file to preview sites - How to preview your websites without changing your name servers.

How to edit your local hosts file to test websites
For a long time now Apache's mod_userdir has been the preferred way to check if sites are working properly on a machine without DNS being configured. This allows users to confirm a site is functional before the DNS is pointed to that system, allowing the site to be tested before it goes live.

However, there are two reasons that mod_userdir may no...
Read more about this resource...
 

TwistAndShout

Active Member
Mar 2, 2021
39
6
8
Paris
cPanel Access Level
Root Administrator
Hello,

I have windows 10, I followed exactly the steps and it worked during several days.

However, it suddenly stoped working. I have flushed my cache several time, I have tried with firefox and Chrome, but suddenly my computer doesn't seem to bite the bait anymore.

This is my host setting : Host setting.JPG.

Then this is the result when I run the command ping -c2 example.com Command test dns.JPG.

Is there any knowned reason why editing local host file would stop working ?

Thanks in advanced.
 
Last edited:

IGSteven

Member
Mar 3, 2018
9
1
3
Coventry
cPanel Access Level
DataCenter Provider
Twitter
Hello,

I have windows 10, I followed exactly the steps and it worked during several days.

However, it suddenly stoped working. I have flushed my cache several time, I have tried with firefox and Chrome, but suddenly my computer doesn't seem to bite the bait anymore.

This is my host setting : Host setting.JPG.

Then this is the result when I run the command ping -c2 example.com Command test dns.JPG.

Is there any knowned reason why editing local host file would stop working ?

Thanks in advanced.
Are you running a note pad as ADMINISTRATOR?

If you open CMD and ping the domain what result do you get? there a chance chrome and firefox has cached the DNS record and the TTL has not expired.
 

TwistAndShout

Active Member
Mar 2, 2021
39
6
8
Paris
cPanel Access Level
Root Administrator
Are you running a note pad as ADMINISTRATOR?

If you open CMD and ping the domain what result do you get? there a chance chrome and firefox has cached the DNS record and the TTL has not expired.
Yes, I am running as admin, the screenshot I provided shows that the ping says "Ip adress must be specified".
 

IGSteven

Member
Mar 3, 2018
9
1
3
Coventry
cPanel Access Level
DataCenter Provider
Twitter
Yes, I am running as admin, the screenshot I provided shows that the ping says "Ip adress must be specified".
I have executed a command ipconfig /flushdns , then I flushed the cache, then I closed the browser. Still, the browser directs me to the curent dns.
Here is a useful post that might help you, I suspected '5. Permissions' is what you want to try.
 

cPDavidL

Linux Analyst II
Oct 15, 2012
79
18
133
cPanel Access Level
Root Administrator
@TwistAndShout
Depending on what browser you're using, you may want to check for an option like "Dns over https"(or something similar). While generally helpful in avoiding tracking and dns poisoning, having this enabled can sometimes prevent the use of a hosts file to preview sites. Some VPN extensions may also force such behavior.
 

TwistAndShout

Active Member
Mar 2, 2021
39
6
8
Paris
cPanel Access Level
Root Administrator
I suspected '5. Permissions' is what you want to try.
Thanks @IGSteven.

I have followed the step 5, wich is creating a new hosts file, copying & pasting the content of the actual one, and then overwriting the actual hosts in \System32\drivers\etc .

The problem is : the actual hosts file is not a .TXT. So when I copy the hosts.txt file that I have created, it doesn't overwrite the actual hosts file. At the end, I have 2 host files with different extensions. Hosts files in directory.png.

Any tip ?

Thanks in advanced.
 

TwistAndShout

Active Member
Mar 2, 2021
39
6
8
Paris
cPanel Access Level
Root Administrator
@TwistAndShout
you may want to check for an option like "Dns over https"(or something similar).
Thanks @cPDavidL .

I have uncheked the DOH DOH deactivated.png, closed Chrome, flushed the cache, but still, Chrome would not take the DNS I indicated.

It's strange because it was perfectly working 2 weeks ago, but now it simply stoped.

Any suggestions ?

Thanks in advanced.
 

IGSteven

Member
Mar 3, 2018
9
1
3
Coventry
cPanel Access Level
DataCenter Provider
Twitter
I have followed the step 5, wich is creating a new hosts file, copying & pasting the content of the actual one, and then overwriting the actual hosts in \System32\drivers\etc .

The problem is : the actual hosts file is not a .TXT. So when I copy the hosts.txt file that I have created, it doesn't overwrite the actual hosts file. At the end, I have 2 host files with different extensions. Hosts files in directory.png.
Hello @TwistAndShout

What text editor / IDE are you using? if you are using notepad, just don't, Ideally, you want something that supports UNICODE characters, to make sure you have not accidentally copied a control character.

Make sure when you are saving the file you change the 'Save as type:' to 'All Files (*.*)' then don't give it a subfix to the end of the file.

Steve,
 

TwistAndShout

Active Member
Mar 2, 2021
39
6
8
Paris
cPanel Access Level
Root Administrator
What text editor / IDE are you using?
Thanks, but it's still not working, wich makes me thinking that I did something wrong. Here is what I do.

1- I open Notepad as admin Step 1.png.
2- I open the hosts file Step 2.JPG.
3- I copy the content, wich includes the DNS where I want my browser to point for the domain I am working on Step3.JPG.
4- I paste the content in a new created file with Sublime Text Step4.png.
5- I save as "all files" Step5.png.
6- I copy it from my desktop Step6.png.
7- I paste it on the destination folder system32/drivers/etc Step7.png and Step8.png.

Then I close Chrome, I reopen, I flush the cache, I go to the URL, but still, Chrome directs me to the actul DNS, not the to the dev server.

Did I miss anything ?

Thanks in advanced.
 
Last edited:

cPRex

Jurassic Moderator
Staff member
Oct 19, 2014
6,044
773
313
cPanel Access Level
Root Administrator
I'm not sure why you're doing the copy/paste and making a new file. I would definitely recommend creating a backup of the original file by copying it to hosts.original or something, but I'd just edit the main hosts file in place instead of trying to copy and replace and all that, as that is where the confusion is happening.
 

TwistAndShout

Active Member
Mar 2, 2021
39
6
8
Paris
cPanel Access Level
Root Administrator
I'm not sure why you're doing the copy/paste and making a new file.
Just following a tutorial provided in this thread...

I'd just edit the main hosts file in place instead of trying to copy and replace and all that, as that is where the confusion is happening.
I did edit the main host file, for 2 websites I am trying to migrate : emprunter-malin.com and c-moins-cher.com.

Editing the host file to point to 51.178.50.143 c-moins-cher.com works fine, Chrome follows this ip and I can edit the destination site without having to change the DNS.

However when I point to 51.178.50.143 emprunter-malin.com Chrome doesn't follow this IP, it goed to the actual IP.

So this setting is working only for one site. Any idea why ?

Thanks in advanced.
 

TwistAndShout

Active Member
Mar 2, 2021
39
6
8
Paris
cPanel Access Level
Root Administrator
Yes, I flush Chrome, I restart my computer, but Chrome simply doesn't follow the IP indicated on hosts.

Let's not insist on things that don't work, I am simply going to work on another URL, and migrate the result later.

Thanks for your help.