I want the hosts file to block www.url.com/page without blocking www.url.com

step 1
I've added www.apple.com to my /etc/host file

When I do ping www.apple.com I get this desired result:

64 bytes from localhost (

When I try to access www.apple.com in the browser, it gives me the desired error and I cannot access the site www.apple.com

So there is no problem with my browsers cache.

step 2
Next Ive added /itunes/ to the line above which makes www.apple.com/itunes/

When I do ping www.apple.com/itunes/ I get the desired result

64 bytes from localhost (

BUT!!!! When I type www.apple.com/itunes into my browser I am able to connect to apple's itunes site, which is not expected. Writing www.apple.com/itunes/ in my /etc/hosts file should send me to when I type www.apple.com/itunes/ in my browser

//////////////// the /etc/hosts file is ignoring suburls like /itunes/ or /ipod/ but works completely fine with domains such as www.apple.com

Why is www.apple.com/itunes/ causing the desired result and www.apple.com not causing the desired result?

Don't tell me browser cache because127.0.0.1 www.apple.com causes the desired result.

UPDATE I have since found out that it is impossible to do what I am trying to do with the hosts file.

The /etc/hosts file can only redirect domains and con not direct pages inside those domains.

answer solved. thank you.


Host file as the name implies, is for HOST, not URL. So apple.com/itunes means nothing. It's not a host.

  • so it's impossible right! – Joshua Robison Apr 29 '13 at 3:29
  • With the hosts file? Yes. It's not fully impossible but by using the hosts file it is. What you're trying to do requires you to mess with DNS. – Will.Beninger Apr 29 '13 at 3:36
  • DNS or Proxy would work. – ETL Apr 29 '13 at 13:36
  • Isn't DNS also for hosts? – Mark Oct 24 '16 at 10:09
  • @Mark - correct. DNS wouldn't solve the URL portion. It could redirect apple.com to some other server but not just some portion of the web site. – ETL Oct 24 '16 at 14:43

You are asking an impossible question.

You are asserting that the correct answer is incorrect, and that we can't tell you that.

Further, your understanding of "ping" is limited - you can't ping a URL, only an IP address or domain name. That it works at all is because it is doing some kind of truncation.

In order to understand just how far base your assertion is, you need to understand how a web page request works.

When a URL is entered into the browser, the browser breaks it down into parts, extracting the domain name. It then resolves the domain name into the IP address. (Thats where the hosts file was used).

After this, the browser connects to the IP address, and issues a "GET" command with the remainder of the URL (if its using HTTPS, it does this in an encrypted session). [ FWIW it also specifies to the web server the domain name it used so that the fileserver can support multiple domain names on 1 IP ]

The problem thus has to be in the browser itself.

Your post also appears somewhat contradictory with respect of what you put in the hosts file.

You can't correctly put a URL (or "suburl") as you called it. It is conceivable that this is where your problem occurred, but without knowing exactly what you typed in, that behaviour can't be reproduced.

  • are you saying that there is no way to redirect apple.com/itunes to using the hosts file? – Joshua Robison Apr 29 '13 at 3:25
  • I don't see why this question gets marked down since I think many other people have a similar question. How can i block www.url.com/innerpage without blocking www.url.com and without using a proxy – Joshua Robison Apr 29 '13 at 3:27
  • 1
    Yes, I'm saying you can't redirect a url using a hosts file unless you do it for every url including that domain name. – davidgo Apr 29 '13 at 3:32
  • I marked the question down because it forbade the correct answer and was unclear. You did not ask how to block a URL without a proxy (doing that practically will require a browser plugin, or technology even harder to set up then a proxy. Maybe you should ask that as a separate question though.) – davidgo Apr 29 '13 at 3:34
  • the reason it's unclear is because whenever I shorten the question it is assumed that I only want to block a domain or that browser caching is the culprit . I had to get rid of all the assumptions. The simple answer to my question is that the /etc/hosts file does not work with pages inside of domains. That's my answer. I had no idea. I thought I could put www.url.com/page inside the hosts file. but it doesnt work like that. thank you – Joshua Robison Apr 29 '13 at 3:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.