Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Saied 349 posts 674 karma points
    Nov 01, 2016 @ 13:20
    Saied
    0

    Redirect rule from url with subdomain to https?

    How would I write the folowing rewrite rules for the following urls:

    dev.site.com => https:\\dev.site.com

    http:\\dev.site.com => https:\\dev.site.com

    qa.site.com => https:\\qa.site.com

    http:\\qa.site.com => https:\\qa.site.com

    dev-sub.site2.com => https:\\dev-sub.site2.com

    http:\\dev-sub.site2.com => https:\\dev-sub.site2.com

    The sites will either have dev., qa. or dev- in the url as well as a different domain site1 or site2

  • Aditya 24 posts 128 karma points
    Nov 01, 2016 @ 15:59
    Aditya
    0

    Try this in your web.config

        <system.webServer>
    
        <rewrite>
          <rules>
            <!--BEGIN rule TAG FOR HTTPS REDIRECT -->
            <rule name="Force HTTPS" enabled="true">
              <match url="(.*)" ignoreCase="false" />
              <conditions>
                <add input="{HTTPS}" pattern="off" />
              </conditions>
              <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />
            </rule>
            <!--END rule TAG FOR HTTPS REDIRECT -->
          </rules>
        </rewrite>
      </system.webServer>
    
  • Saied 349 posts 674 karma points
    Nov 01, 2016 @ 16:46
    Saied
    0

    Hi Can you explain what is happening here like what does {R:1} mean?

    What if I want to add www to a url like test.com so it becomes https://www.test.com, but dev.test.com would just become https://dev.test.com?

    Thanks

  • Aditya 24 posts 128 karma points
    Nov 02, 2016 @ 05:29
    Aditya
    0

    Hey,

    This rule will cover your use cases - it takes http://<anything>.domain.com and redirects that to https://<anything>.domain.com.

    However, you do need to have CNAMES set up for the sub domains that you want. www is commonly added by default at many registrars, but stuff like dev.xyz.com and all will need to be added via your domain DNS control panel.

    Do you have your DNS properly set up? Redirect rules won't help if DNS doesn't know where to send your sub-domains.

    And here's a good resource to learn about IIS Redirects. This will explain the {R:1} and the rest of it

    https://www.iis.net/learn/extensions/url-rewrite-module/url-rewrite-module-configuration-reference

    Specifically, look at this part

    A Redirect action has the following configuration options:

    url – Uses a substitution string as a redirection URL. A substitution URL is a string that can include the following: Back-references to the condition and rule patterns. (For more information, see the section about how to use back-references.)

    So the R:1 rule is simply rewriting whatever URL you put in, just changing the http -> https. This is sent back to the users browser as a permanent redirect, and the browser opens the SSL site. All this happens nearly instantaneously.

    It's a bit long ,but go through it and see if any of it makes sense. Feel free to ask questions after

Please Sign in or register to post replies

Write your reply to:

Draft