35

I know you can edit a hosts file to redirect a URL to an IP address as so:

127.0.0.1 google.com

but how do you force a redirect to a URL instead of an IP address?

e.g.

mysite.com/welcome.aspx google.com

The explicit URL doesn't work and assuming mysite.com's IP is 222.222.222.222, the following doesn't work either:

222.222.222.222/welcome.aspx google.com

Glory
  • 441
  • 1
  • 6
  • 5

6 Answers6

11

No, but you could open a web server at, for example, 127.0.0.77 and use it to check if the Request URI is "/welcome.aspx"... If yes redirect to google, if not load the original site.

127.0.0.77      mysite.com
Raphael R.
  • 23,524
  • 1
  • 22
  • 18
  • 1
    Good point, I've used this as a stopgap ad-blocker for approx. 10 domains (it doesn't scale well for larger number of domains, at least not on Windows) – Piskvor left the building Jul 19 '10 at 13:43
  • 2
    @Piskvor, `hosts` file is supposed to scale to over thousands of lines... Please elaborate what do you mean by "doesn't scale well", Do you get BSODs? – Pacerier May 08 '15 at 03:55
  • 2
    For several years on various devices I've happily been using `HOSTS` to block [**~15,000 domains**](https://someonewhocares.org/hosts/) to make the internet "not suck as much" . . . and I'm about to add the [**~2,100 domains**](https://raw.githubusercontent.com/jmdugan/blocklists/master/corporations/facebook/all) that Facebook uses track us (even when logged out or after account deletion *#%$@! buggers*) ...so, I promise you it will work for more than 10. – ashleedawg Nov 12 '19 at 21:15
9

You can't. A redirect requires a webserver to accept the first request and send back the redirect. The "hosts" file just lets you set your own DNS records.

David
  • 34,223
  • 3
  • 62
  • 80
4

Apply this trick.

First you need IP address of url you want to redirect to. Lets say you want to redirect to stackoverflow.com To find it, use the ping command in a Command Prompt. Type in:

ping stackoverflow.com

into the command prompt window and you’ll see stackoverflow's numerical IP address. Now use that IP into your host file

104.16.36.249 google.com

yay now google is serving stackoverflow :)

Robert
  • 5,278
  • 43
  • 65
  • 115
ajitpawarink
  • 436
  • 6
  • 14
  • 18
    I know this thread is quite old, but I would like add a note here. This answer will **never work** if the server uses CDN or virtual hosts. This **only applies** on servers with **dedicated IPs**. This answer seems correct, but it is **totally wrong** for the case of StackOverflow. It uses Cloudflare as CDN –  Apr 04 '17 at 15:22
0

hosts file:

1.2.3.4 google.com

1.2.3.4 - ip of your server.

Run script on the server for redirecting users to url that you want.

Pavel Strakhov
  • 39,123
  • 5
  • 88
  • 127
0

You could use the RedirectMatch directive in Apache to do something similar you want.

It's pretty simple.

RedirectMatch / http://222.222.222.222/

Anyway, I can't see any reason to do that thing. Aren't you trying to intercept traffic? There are better ways. For Linux boxes as a router: iptables -j REDIRECT + Squid or Apache. For Cisco routers, you can use WCCP to a Cache or Web Server...

KikoV
  • 899
  • 6
  • 13
0

Make sure to double the entry with an additional "www"-prefix. If you don't addresses like "www.acme.com" will not work!

Martin.Martinsson
  • 1,894
  • 21
  • 25