How to use a subdirectory as a root for domain

Discussion in 'ASP.NET / ASP.NET Core' started by Stefanie Orozco, Sep 10, 2015.

  1. I'm very new to setting up web.config files so I'm hoping this is a simple fix.

    I have two domains for an account:
    www.primarydomain.com - points to root
    www.secondarydomain.com

    I'd like to have secondarydomain.com have its files in a subdirectory off root:
    /secondarydomain

    I read this article:
    http://blog.discountasp.net/root-redirection-to-subdirectory/

    I updated the web.config file as such:
    ******************
    <rewrite>
    <rules>
    <rule name="secondarydomain" stopProcessing="true">
    <match url="^$" />
    <conditions>
    <add input="{HTTP_HOST}" pattern="^(www.)?secondarydomain.com" />
    <add input="{PATH_INFO}" pattern="^/secondarydomain/" negate="true" />
    </conditions>
    <action type="Rewrite" url="/secondarydomain" />
    </rule>
    </rules>
    </rewrite>
    ******************

    It works but it displays the subdirectory name when you hit the url:
    www.secondarydomain.com/secondarydomain

    I don't want to show the subdirectory folder name (/secondarydomain).

    How do I fix my web.config to use /secondarydomain as if it were the root directory for www.secondarydomain.com without showing that secondary folder name in the URL?
     
  2. RayH

    RayH DiscountASP.NET Lackey DiscountASP.NET Staff

    Try this:

    Code:
    <rewrite>
      <rules>
        <rule name="Rewrite to secondarydomain" stopProcessing="true">
          <match url="(.*)" />
          <conditions>
            <add input="{HTTP_HOST}" pattern="^www.secondarydomain.com$" />
          </conditions>
          <action type="Rewrite" url="subfolder_name/{R:1}" />
        </rule>
      </rules>
    </rewrite>
    
    Replace secondarydomain and subfolder_name with the appropriate values.
     
    mjp and Stefanie Orozco like this.
  3. This caused a 404 error. Here is the detail:
    Detailed Error Information:
    Module IIS Web Core
    Notification MapRequestHandler
    Handler StaticFile
    Error Code 0x80070002
    Requested URL http://www.secondarydomain.com:80/subfolder_name/subfolder_name/
    Physical Path E:\web\primarydomain\htdocs\subfolder_name\subfolder_name\
    Logon Method Anonymous
    Logon User Anonymous

    I was able to update the code to stop the 404 by changing the match URL value to it's previous value:
    Code:
    <rewrite>
      <rules>
        <rule name="Rewrite to secondarydomain" stopProcessing="true">
    <match url="^$" />
    <conditions>
    <add input="{HTTP_HOST}" pattern="^www.secondarydomain.com$" />
    </conditions>
    <action type="Rewrite" url="subfolder_name/{R:1}" />
    </rule>
    </rules>
    </rewrite>

    But it doesn't mask the /subfolder_name/ in the url (and also shows it with two forward slashes). Here's what I get when I hit the www.secondarydomain.com:
    http://www.secondarydomain.com//subfolder_name/
     
  4. Ugh...I just removed the rewrite rule and noticed the site still re-routes to that folder. Thus, somewhere there is a redirect already happening (which would then explain the double folder in the previous 404 error). I'm hunting down where that configuration is at (although I thought the only place could be in the web.config root folder file).
     
  5. RayH

    RayH DiscountASP.NET Lackey DiscountASP.NET Staff

    I just tested the code I offered, and it works fine. And yes, there must be another redirection somewhere.
     
    Stefanie Orozco likes this.
  6. Just figured out it was a caching issue on my end.
    Code you provided worked like a charm.
    Thank you!
     
    mjp and RayH like this.

Share This Page