windows 编辑主机文件以重定向网址?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3266483/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Editing hosts file to redirect url?
提问by Glory
I know you can edit a hosts file to redirect a URL to an IP address as so:
我知道您可以编辑主机文件以将 URL 重定向到 IP 地址,如下所示:
127.0.0.1 google.com
127.0.0.1 google.com
but how do you force a redirect to a URL instead of an IP address?
但是如何强制重定向到 URL 而不是 IP 地址?
e.g.
例如
mysite.com/welcome.aspx google.com
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:
显式 URL 不起作用,假设 mysite.com 的 IP 是 222.222.222.222,以下也不起作用:
222.222.222.222/welcome.aspx google.com
222.222.222.222/welcome.aspx google.com
回答by David
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.
你不能。重定向需要网络服务器接受第一个请求并将重定向发回。“hosts”文件只允许您设置自己的 DNS 记录。
回答by Raphael R.
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 处打开一个 Web 服务器并使用它来检查请求 URI 是否为“/welcome.aspx”...如果是,则重定向到 google,如果不加载原始站点。
127.0.0.77 mysite.com
回答by BKY
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:
首先,您需要要重定向到的 url 的 IP 地址。假设您想重定向到 stackoverflow.com 要找到它,请在命令提示符中使用 ping 命令。输入:
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
进入命令提示符窗口,您将看到 stackoverflow 的数字 IP 地址。现在将该 IP 用于您的主机文件
104.16.36.249 google.com
104.16.36.249 google.com
yay now google is serving stackoverflow :)
是的,现在谷歌正在为 stackoverflow 提供服务 :)
回答by Martin.Martinsson
Make sure to double the entry with an additional "www"-prefix. If you don't addresses like "www.acme.com" will not work!
确保使用额外的“www”前缀将条目加倍。如果你没有像“www.acme.com”这样的地址将不起作用!
回答by KikoV
You could use the RedirectMatch directive in Apache to do something similar you want.
您可以使用 Apache 中的 RedirectMatch 指令来执行您想要的类似操作。
It's pretty simple.
这很简单。
RedirectMatch / http://222.222.222.222/
重定向匹配/ 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...
无论如何,我看不出有什么理由去做那件事。你不是想拦截流量吗?有更好的方法。对于作为路由器的 Linux 机器:iptables -j REDIRECT + Squid 或 Apache。对于 Cisco 路由器,您可以使用 WCCP 到缓存或 Web 服务器...
回答by Pavel Strakhov
hosts file:
主机文件:
1.2.3.4 google.com
1.2.3.4 - ip of your server.
1.2.3.4 - 您服务器的 IP。
Run script on the server for redirecting users to url that you want.
在服务器上运行脚本以将用户重定向到您想要的 url。