How can I redirect a subdomain to a sub directory?

Discussion in 'Site Programming, Development and Design' started by mzagarri, Mar 6, 2014.

  1. I've been testing many different ways to do this and I cannot get it quite right. I was working on this yesterday and what I am trying to accomplish is redirecting "subdomain.domain.com" to a sub folder in my root. I put the following code in the root config file, but I had no luck.

    This is what I have:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
    <system.webServer>
         <modules runAllManagedModulesForAllRequests="true" />
    <rewrite>
        <rules>
            <rule name="subdomain.domain.com" stopProcessing="true">
                <match url=".*" />
                <conditions>
                    <add input="{HTTP_HOST}" pattern="^subdomain.domain.com$" />
                    <add input="{PATH_INFO}" pattern="^\subdomain\.com$" negate="true" />
                </conditions>
                <action type="Rewrite" url="/Apply/subdomain/ {R:0}" />
            </rule>
        </rules>
    </rewrite>  
      </system.webServer>
    </configuration>
    I am want to point it to a sub directory within a subdirectory of the root. I am sur eI am going about this all wrong as you can see I am trying it with "<action type="Rewrite" url="/Apply/subdomain/ {R:0}" />".

    Any help would be appreciated.
     
  2. Elshadriel

    Elshadriel Winhost Staff

    Have you tried setting up the rules with the URL Rewrite module in IIS7 Manager? It might be difficult manually editing the web.config file if you don't use Regular Expressions regularly.
     
    Last edited by a moderator: Oct 14, 2015
  3. I did find an article on IIs7 manager, but I became lost so, I went with the method above. I am very new to this and I do not want to muck up any of the sites already in place.
     
  4. Tabitha

    Tabitha WinHost HBIC

    When you create redirect/rewrite rules in IIS Manager, it just writes them to the web.config file(s) of your application(s). So, if you back up the web.config files (e.g., download them to your local computer with FileZilla), you can "roll back" any changes you make if they don't work.

    Also, are you actually trying to hit a directory two levels down from the root? You said both "what I am trying to accomplish is redirecting "subdomain.domain.com" to a sub folder in my root. " and "I am want to point it to a sub directory within a subdirectory of the root.", and your code is referring to a directory 2 levels down, so I just want to clarify.

    From just a cursory glance, this line:

    <action type="Rewrite" url="/Apply/subdomain/ {R:0}" />

    should not have a space between "/" and "{R:0}".

    I had an account where this code was working (with an actual domain name of course, not "domain.com"):

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

    YMMV. URL rewrite is tricky.
     
    ComputerMan and Michael like this.
  5. It is two levels down from the root. So I assume the following:

    Code:
    <add input="{PATH_INFO}" pattern="^/subdirectory/subdirectory/" negate="true" />
    </conditions>
    <action type="Rewrite" url="\subdirectory\subdirectory\{R:0}" />
     
    Last edited: Mar 6, 2014
  6. Tabitha

    Tabitha WinHost HBIC

    That looks like it should work. Did you have any luck?
     
    ComputerMan likes this.
  7. I am facing similar issue.
    I have updated my web.config file - and uploaded this web.config file inside the subfolder where my subdomain is suppose to point to
    but the browser shows the directory listing when trying to open subdomain.domain.org
    there is no application files or web.config at the root. I have multiple subfolders in the root.

    <system.webServer>
    <rewrite>
    <rules>
    <rule name="subdomain.domain.org " stopProcessing="true">
    <match url=".*" />
    <conditions>
    <add input="{HTTP_HOST}" pattern="^subdomain.domain.org $" />
    <add input="{PATH_INFO}" pattern="^/subfolder_Prod/" negate="true" />
    </conditions>
    <action type="Rewrite" url="\subfolder_Prod\{R:0}" />
    </rule>
    </rules>
    </rewrite>
    </system.webServer>
     
  8. Elshadriel

    Elshadriel Winhost Staff

    The web.config file with the URL Rewrite rules goes in the root folder.
     

Share This Page