简体   繁体   English

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

[英]Asp.net 4 url rewrite a subdomain as a folder

What I'm trying to do is the following, I want to rewrite this kind of url:我想做的是以下,我想重写这种url:

blog.domain.com/...

into进入

domain.com/blog/...

It's in a shared host environment, using IIS7/ASP.Net 4. Another thing is that both the domain and the blog subdomain have different aps running.它在共享主机环境中,使用 IIS7/ASP.Net 4。另一件事是域和博客子域都有不同的 aps 运行。 I've been searching for hours for what's the best solution here, and I hope someone can guide me a bit here.我一直在寻找这里最好的解决方案几个小时,我希望有人可以在这里指导我。 Thanks!谢谢!

Here is an attempt as first idea.这是第一个想法的尝试。

    // 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;
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM