简体   繁体   中英

Url rewrite for subdomain as Username


first of all i do not have enough experience with Url rewriting in asp.net. Anyways i pulled url rewriting thanks to SO and other helpful articles over internet. But this new project have a specific need, i have to show Url for different users as:

username.domainname.topdomain

further more every action for user specific pages should work like this ie for user profile.aspx page instead of :

domain.topdomain/username/profile.aspx

we want:

username.domainname.topdomain/profile/

how can i pull this out?
Regards
PS the request can have "N" number of querystrings ,so anything in web.config or so will be too complex to handle.

    //DLL: Intelligencia.UrlRewriter.dll    

    //web.config
    <configuration>

      <configSections>
        <section name="rewriter" requirePermission="false" type="Intelligencia.UrlRewriter.Configuration.RewriterConfigurationSectionHandler, Intelligencia.UrlRewriter"/>
      </configSections>
    </configuration>

    <system.web>
        <httpModules>
          <add name="UrlRewriter" type="Intelligencia.UrlRewriter.RewriterHttpModule, Intelligencia.UrlRewriter"/>
        </httpModules>
    </system.web>


    <rewriter>
        <rewrite url="~/(.+)/CompanyHomePage" to="~/Home.aspx"/>
    </rewriter>


    //C#:

     string strTitle = Session["company_name"].ToString();
     strTitle = strTitle.Trim('-');
     strTitle = strTitle.ToLower();
     char[] chars = @"$%#@!*?;:~`+=()[]{}|\'<>,/^&"".".ToCharArray();
     strTitle = strTitle.Replace("c#", "C-Sharp");
     strTitle = strTitle.Replace("vb.net", "VB-Net");
     strTitle = strTitle.Replace("asp.net", "Asp-Net");
     strTitle = strTitle.Replace(".", "-");

    for (int i = 0; i < chars.Length; i++)
    {
        string strChar = chars.GetValue(i).ToString();
        if (strTitle.Contains(strChar))
        {
           strTitle = strTitle.Replace(strChar, string.Empty);
        }
    }
     strTitle = strTitle.Replace(" ", "-");
     strTitle = strTitle.Replace("--", "-");
     strTitle = strTitle.Replace("---", "-");
     strTitle = strTitle.Replace("----", "-");
     strTitle = strTitle.Replace("-----", "-");
     strTitle = strTitle.Replace("----", "-");
     strTitle = strTitle.Replace("---", "-");
     strTitle = strTitle.Replace("--", "-");
     strTitle = strTitle.Trim();
     strTitle = strTitle.Trim('-');

     Response.Redirect("~/" + strTitle + "/CompanyHomePage", false);//Redirect to ~/Home.aspx page


//reference: CompanyHomePage same in web.config  <rewrite url="~/(.+)/CompanyHomePage" to="~/Home.aspx"/> which company is log in to sight that company name show in url like this http://abcd//CompanyHomePage

The HttpContext class in Asp.Net will not help you with this one. The rewriter methods on that are intended to cover the path and query string, not the hostname or port.
I believe you will need to use a client-side re-write in order to change the hostname: send some script to the client to reload from a different location.

As others have said, you will need to have some wildcard DNS setup to allow your username.domain.tld hostnames resolve to your web server.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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