locked
Rewriting Subdomain to Folder with Full URL RRS feed

  • Question

  • User774606560 posted

    Hi!

    Is it possible to rewrite something like this:

    http://sub.domain.com/resources/default.htm

    to this:

    http://domain.com/sub/resources/default.htm

    Any help would be great!

    Thanks!

    -Eric

    Tuesday, July 3, 2012 2:03 PM

Answers

  • User-619846739 posted

    Hi Eric,

    Sure, you can do a redirect like this:

    <rule name="Subdomain redirect" stopProcessing="true">
        <match url=".*" />
        <conditions>
            <add input="{HTTP_HOST}" pattern="^www\.localtest\.me$" negate="true" />
            <add input="{HTTP_HOST}" pattern="^([^\.]+)\.localtest\.me$" />
        </conditions>
        <action type="Redirect" url="http://localtest.me/{C:1}{URL}" />
    </rule>

     The other option is a rewrite which won't change the domain name but will call /sub/… in the background and preserve the original URL.  That rule would look like this:

    <rule name="Subdomain rewrite" stopProcessing="true">
        <match url=".*" />
        <conditions>
            <add input="{HTTP_HOST}" pattern="^www\.localtest\.me$" negate="true" />
            <add input="{HTTP_HOST}" pattern="^([^\.]+)\.localtest\.me$" />
        </conditions>
        <action type="Rewrite" url="/{C:1}{URL}" />
    </rule>

    • Marked as answer by Anonymous Tuesday, September 28, 2021 12:00 AM
    Tuesday, July 3, 2012 2:15 PM

All replies

  • User-619846739 posted

    Hi Eric,

    Sure, you can do a redirect like this:

    <rule name="Subdomain redirect" stopProcessing="true">
        <match url=".*" />
        <conditions>
            <add input="{HTTP_HOST}" pattern="^www\.localtest\.me$" negate="true" />
            <add input="{HTTP_HOST}" pattern="^([^\.]+)\.localtest\.me$" />
        </conditions>
        <action type="Redirect" url="http://localtest.me/{C:1}{URL}" />
    </rule>

     The other option is a rewrite which won't change the domain name but will call /sub/… in the background and preserve the original URL.  That rule would look like this:

    <rule name="Subdomain rewrite" stopProcessing="true">
        <match url=".*" />
        <conditions>
            <add input="{HTTP_HOST}" pattern="^www\.localtest\.me$" negate="true" />
            <add input="{HTTP_HOST}" pattern="^([^\.]+)\.localtest\.me$" />
        </conditions>
        <action type="Rewrite" url="/{C:1}{URL}" />
    </rule>

    • Marked as answer by Anonymous Tuesday, September 28, 2021 12:00 AM
    Tuesday, July 3, 2012 2:15 PM
  • User774606560 posted

     Thanks, Scott! I figured there had to be a way. This will help transition our current URL format to the new one. Great stuff!

    Tuesday, July 3, 2012 2:17 PM
  • User319397123 posted
    It is not working in my website. Please help me ASAP. Thanks
    Saturday, July 14, 2012 3:19 AM
  • User1892921663 posted
    Hi Scott, I have two web site on separate server with the same domain having different sub-domain like: subdomain1.mysite.com subdomain2.mysite.com I have to rewrite as mysite.com/subdomain1, I have applied the rules and getting the bad request error 503. Please Help.... Thanks, Rajan
    Tuesday, September 11, 2012 10:48 AM
  • User-619846739 posted
    Hi Rajan, Did you run the redirect or the rewrite rule? Can you visit the new URL directly (e.g. mysite.com/subdomain1) and does it work? Scott
    Tuesday, September 11, 2012 12:02 PM
  • User1892921663 posted
    Scott, I have written the rule for re-write the url. Thanks Rajan
    Wednesday, September 12, 2012 9:36 AM
  • User-619846739 posted
    Rajan, It sounds like you're using a reverse proxy rule. You haven't provided enough information for me to tell much. I would suggest looking at Failed Request Tracing (FRT) so that you can see what the original request looks like, and what the rewritten request look like. Any further information that you can provide will help too. For example, are you using ARR, or just a reverse rewrite rule, and can you provide the rule?
    Wednesday, September 12, 2012 9:46 AM