locked
Rewrite rule for accessing page in main domain from subdomain RRS feed

  • Question

  • User-1604169926 posted

    I want a rewrite rule for accessing main domain page from subdomain.

    What I want is when someone type url as "api.mydomain.com/..." then the file in my current domain should be accessed.

    for example.

    URL to be typed : api.mydomain.com/page/abc.aspx

    Actual Address of that page : www.mydomain.com/page/abc.aspx

    I want rewrite rule, not redirect. I don't want that the URL should be redirected to actual address. URL would remain api.mydomain.com, it should not be changed.

    Saturday, November 16, 2013 5:54 AM

Answers

  • User-1454326058 posted

    Hi pratik_galor,

    Thanks for your post!

    For your requirement, the code will be like this:

    <rewrite>
            <rules>
              <rule name="RewriteDomain" stopProcessing="true">
                <match url="*" />
                <conditions>
                  <add input="{HTTP_HOST}" pattern="^api.mydomain.com" />
                </conditions>
                <action type="Rewrite" url="http://www.mydomain.com/{R:0}" />
              </rule>
            </rules>
          </rewrite>

    For more information, please refer to http://weblogs.asp.net/owscott/archive/2009/11/27/iis-url-rewrite-rewriting-non-www-to-www.aspx

    Note: the value of action type should be Rewrite.

    Thanks

    Best Regards

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 18, 2013 1:44 AM

All replies

  • User-1454326058 posted

    Hi pratik_galor,

    Thanks for your post!

    For your requirement, the code will be like this:

    <rewrite>
            <rules>
              <rule name="RewriteDomain" stopProcessing="true">
                <match url="*" />
                <conditions>
                  <add input="{HTTP_HOST}" pattern="^api.mydomain.com" />
                </conditions>
                <action type="Rewrite" url="http://www.mydomain.com/{R:0}" />
              </rule>
            </rules>
          </rewrite>

    For more information, please refer to http://weblogs.asp.net/owscott/archive/2009/11/27/iis-url-rewrite-rewriting-non-www-to-www.aspx

    Note: the value of action type should be Rewrite.

    Thanks

    Best Regards

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 18, 2013 1:44 AM
  • User-1853252149 posted

    URL would remain api.mydomain.com, it should not be changed.

    Can't do that with a rewrite.  You are rewriting the URL, so it will change.

    I actually can't think of a reason anyone would set up a domain structure like this.  Simply point api.domain.dom to the same IP as www.domain.com in DNS.  Done.

    Jeff

    Tuesday, November 19, 2013 2:08 PM
  • User-1604169926 posted

    @Starain

    I have setup "api" subdomain also, do I have to delete it?

    and in which web.config do I have to write this? web.config of "api" subdomain or main domain?

    Sunday, December 1, 2013 10:05 AM
  • User-1604169926 posted

    @Jeff

    Actually, I have dlls in main domain and what I want to do is to develop API of my web app.. Now I want to provide api.mydomain.com url for API.

    I dont want to publish the same code on "api" subdomain, I want that the same code of www domain should be used but URL should be rewritten for api.mydomain.com. Hope you getting me, or suggest me an alternative to achieve the same.

    Sunday, December 1, 2013 10:07 AM
  • User-1454326058 posted

    Hi,

    I have setup "api" subdomain also, do I have to delete it?

    I think you needn't delete it.

    and in which web.config do I have to write this? web.config of "api" subdomain or main domain?

    In your main domain.

    You also can generate the rewrite code via IIS rules wizard, for more information, please refer to:

    http://www.iis.net/learn/extensions/url-rewrite-module/creating-rewrite-rules-for-the-url-rewrite-module

    Thanks

    Best Regards

    Monday, December 2, 2013 3:51 AM
  • User-1853252149 posted

    Actually, I have dlls in main domain and what I want to do is to develop API of my web app.. Now I want to provide api.mydomain.com url for API.

    Ah, got it.  In that case, the given rewrite in the main domain should work for you.

    Jeff

    Monday, December 9, 2013 2:04 PM