Asp.net 4 url​​将子域重写为文件夹

时间:2011-07-02 13:58:09

标签: asp.net url-rewriting

我要做的是以下内容,我想重写这种网址:

blog.domain.com/...

domain.com/blog/...

它位于共享主机环境中,使用IIS7 / ASP.Net 4.另一个原因是域和博客子域都运行不同的aps。我一直在寻找几个小时的最佳解决方案,我希望有人可以在这里指导一下。谢谢!

1 个答案:

答案 0 :(得分:0)

这是第一个想法的尝试。

    // keep a valid list somewhere
    List<string> cValidNames = new List<string>();
    cValidNames.Add("blog");

    // get the host
    //string TheHost = Request.Url.Host;
    string TheHost = "blog.domain.com";

    // find the first part, assume that the domain is standard and not change
    int WhereStarts = TheHost.IndexOf(".domain.com");

    // if we found it
    if(WhereStarts != -1)
    {
        string cTheFirstPart = TheHost.Substring(0, WhereStarts);

        // if its on the valid domain (exclude the www)
        if (cValidNames.Contains(cTheFirstPart))
        {
            // now I add in the frond the name and continue with the rest url
            string cFinalPath = "/" + cTheFirstPart + Request.RawUrl;

            // now rewrite it.
            HttpContext.Current.RewritePath(cFinalPath, false);

            return;
        }
    }