URL Rewrite rule for subdomain hosted in subfolder inside root. [Answered]RSS

1 reply

Last post Jul 05, 2013 06:30 PM by peterviola

  • URL Rewrite rule for subdomain hosted in subfolder inside root.

    Jul 05, 2013 09:28 AM|vipultyagi|LINK

    I have two domains and a sub domain hosted on single server. Below is the hierarchy I am using:

    1. Primary domain:               Domain1.com using root folder( \)
    2. Secondary domain:         Domain2.com using “domain2” folder inside root (\domain2)
      1. Sub domain:       Blogs.domain2.com using “blogs” folder inside domain2 (\domain2\blogs)

    I’ve installed WordPress application in root and sub domain folder which generates 2 identical web.config, one for each instance. Below is the rule settings in both the web.config file.

    <rule name="wordpress" patternSyntax="Wildcard">
    <match url="*"/>
                   <conditions>
                    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true"/>
                    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true"/>
                    </conditions>
                    <action type="Rewrite" url="index.php"/>
    </rule>

    Now problem is whenever I try to open application hosted in sub domain folder, it crashed. I contact hosting service support and they told me this is problem of web.config file and they cannot provide any support since it is coding problem.

    I google this problem and found that it is a problem of rewrite rules in config file. After I changed the rule name in sub domain file to “subdomain” application main page renders perfectly but when I navigate to some other page of sub domain, it redirects me to the application hosted in root directory.  I try changing rules in web.config of root folder and sub domain folder both but could not fix this problem.

    What I want is that if request comes for sub domain application, it should use contents inside the “\domain2\blogs” folder.

     

    I don’t have much idea about Url rewriting module. Please help.

     

  • Re: URL Rewrite rule for subdomain hosted in subfolder inside root.

    Jul 05, 2013 06:30 PM|peterviola|LINK

    Hi,

    If I understand your scenario you're trying to host 3 domains within 1 physical site in IIS. You can do this easily with Url Rewrite using this rule. This rule needs to be put into the web.config of domain1. This rule will run 2 additional domains out of subfolders in domain1.

    <rewrite>
    <rules>
        <rule name="site2.com" stopProcessing="true">
            <match url=".*" />
            <conditions>
                <add input="{HTTP_HOST}" pattern="^(www.)?site2.com" />
                <add input="{PATH_INFO}" pattern="^/site2/" negate="true" />   
            </conditions>       
            <action type="Rewrite" url="\site2\{R:0}" />  
        </rule>
        <rule name="site2.com" stopProcessing="true">
            <match url=".*" />
            <conditions>
                <add input="{HTTP_HOST}" pattern="^(www.)?site3.com" />
                <add input="{PATH_INFO}" pattern="^/site3/" negate="true" />   
            </conditions>       
            <action type="Rewrite" url="\site3\{R:0}" />  
        </rule>
    </rules>
    </rewrite>

    However in your example you indicate that site 3 needs to run from a subfolder of site 2 (which is in a subfolder of site 1). So then this slightly different version should work. The rule for site 2 is the same as above. Again this needs to be copied into the web.config of site 1.

    <rewrite>
    <rules>
        <rule name="site2.com" stopProcessing="true">
            <match url=".*" />
            <conditions>
                <add input="{HTTP_HOST}" pattern="^(www.)?site2.com" />
                <add input="{PATH_INFO}" pattern="^/site2/" negate="true" />   
            </conditions>       
            <action type="Rewrite" url="\site2\{R:0}" />  
        </rule>

        <rule name="site3.com" stopProcessing="true">
            <match url=".*" />
            <conditions>
                <add input="{HTTP_HOST}" pattern="^(www.)?site3.com" />
                <add input="{PATH_INFO}" pattern="^/site2/site3/" negate="true" />   
            </conditions>       
            <action type="Rewrite" url="\site2\site3\{R:0}" />  
        </rule>
    </rules>
    </rewrite>

    What I don't understand though is that you indicate you've got your own server so then you should be able to just create 3 separate sites in IIS and not have to deal with Url Rewrite at all. That would make your life much easier.

    Peter Viola
    MCTIP SA, EST, DBA 2008
    MCSA Windows Server 2012

    Please 'Mark as Answer' if this post helps you.

    www.peterviola.com