【问题标题】:How to redirect a url to localhost?如何将 url 重定向到 localhost?
【发布时间】:2012-08-27 22:53:37
【问题描述】:

我想在我的 Windows 2008 R2 服务器上做一些测试。也就是说,让一个 URL 重定向到localhost

例如让"http://mysample.mydomain.com/index.html"实际访问“http://localhost/index.html”。有什么办法可以做到吗?

我尝试编辑windows\system32\drivers\etc\hosts 文件,添加127.0.0.1 -> mysample.mydomain.com 映射,但它不起作用。似乎127.0.0.1localhost 并不相同。 我可以访问“http://localhost/index.html”,但无法访问“http://127.0.0.1/index.html”!

提前致谢!

【问题讨论】:

  • 我认为这应该迁移到 superuser.com

标签: localhost windows-server-2008-r2 ipv6 ipv4


【解决方案1】:

由于您使用的是 Windows 2008 R2 服务器,我猜您正在使用 IIS 进行测试。另请注意,Windows Server 2008 是第一个默认“启用”IPv6 的 Windows 版本 - 但您的 IPv4 版本是否也启用了...?

Enable IPv4: [Control panel > Network and sharing center > Change adapter settings > Right click the adapter used for connectivity and select properties > See if IPv4 is checked. ]

我认为您的 localhost 在内部已被解析为使用 ::1 (IPv6) 环回地址。您可以通过检查来确认:

对于 IPv6 测试:ping localhost -6 和对于 IPv4 测试:ping localhost -4。如果它解析为 ::1,则它使用 IPv6 地址。

您的 \etc\hosts 文件应包含以下条目:

::1 locahost mysample.mydomain.com

现在以ping mysample.mydomain.com -6 进行 ping 测试。这应该确认 mysample.mydomain.com 已解析为您的本地环回 使用 IPv6 的地址。

参考:

  1. Difference-between-127-0-0-1-and-localhost
  2. Localhost

【讨论】:

    【解决方案2】:

    编写继承自 System.Web.UI.Page 的类,然后在加载时覆盖:

    public abstract class CorePage : Page
    {
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
    
            //TODO: check request url and make a redirect if required!
    
        }
    }
    

    将所有页面更改为从 CorePage 继承,工作就完成了!这当然只适用于应用程序级别的 aspx 页面,而不适用于整个 IIS。

    【讨论】:

    • 我想用我的 IE 来做这个。
    猜你喜欢
    • 2014-08-16
    • 2010-10-18
    • 2020-12-11
    • 1970-01-01
    • 2021-03-07
    • 2013-10-10
    • 2019-10-23
    • 1970-01-01
    • 2013-09-14
    相关资源
    最近更新 更多