Search code examples
htmlangulartypescriptiisweb-config

Angular universal web config file with ssl and www redirecting


I have a little problem with the web.config file of my angular universal project. My web.config file is like this;

<configuration>
    <system.webServer>

        <!-- indicates that the server.js file is a node.js application
        to be handled by the iisnode module -->

        <handlers>
            <add name="iisnode" path="main.js" verb="*" modules="iisnode" />
        </handlers>

        <rewrite>
            <rules>
                <rule name="sendToNode">
                    <match url="/*" />
                    <action type="Rewrite" url="main.js" />
                </rule>
            </rules>
        </rewrite>

    </system.webServer>
</configuration>

We are trying to add a www redirecting when there is no www written into link and a https redirecting when there is no https written in the url. But we couldnt manage to do this.

Should I need to do something from the Angular for this?

How can we add those two redirectings into our angular universal web config?


Solution

  • <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <system.web>
        <httpRuntime enableVersionHeader="false" />
      </system.web>
      <system.webServer>
        <httpProtocol>
          <customHeaders>
            <add name="Strict-Transport-Security" value="max-age=31536000"/>
            <add name="X-Content-Type-Options" value="nosniff" />
            <add name="X-Frame-Options" value="DENY" />
            <add name="X-XSS-Protection" value="1; mode=block" />
            <remove name="X-Powered-By" />
          </customHeaders>
        </httpProtocol>
        <webSocket enabled="false" />
        <handlers>
          <!-- Indicates that the main.js file is a node.js site to be handled by the iisnode module -->
          <add name="iisnode" path="main.js" verb="*" modules="iisnode"/>
        </handlers>
        <rewrite>
          <rules>
            <rule name="HTTP to HTTPS redirect" stopProcessing="true">
              <match url="(.*)" />
              <conditions>
                <add input="{HTTPS}" pattern="off" ignoreCase="true" />
              </conditions>
              <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent"/>
            </rule>
            <!-- Do not interfere with requests for node-inspector debugging -->
            <rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">
              <match url="^main.js\/debug[\/]?" />
            </rule>
            <!-- All other URLs are mapped to the node.js site entry point -->
            <rule name="DynamicContent">
              <match url="^(?!.*login).*$"></match>
              <conditions>
                <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true"/>
              </conditions>
              <action type="Rewrite" url="main.js"/>
            </rule>
          </rules>
          <outboundRules>
            <rule name="Add Strict-Transport-Security when HTTPS" enabled="true">
              <match serverVariable="RESPONSE_Strict_Transport_Security" pattern=".*" />
              <conditions>
                <add input="{HTTPS}" pattern="on" ignoreCase="true" />
              </conditions>
              <action type="Rewrite" value="max-age=31536000" />
            </rule>
          </outboundRules>
        </rewrite>
        <!-- 'bin' directory has no special meaning in node.js and apps can be placed in it -->
        <security>
          <requestFiltering>
            <hiddenSegments>
              <remove segment="bin"/>
            </hiddenSegments>
          </requestFiltering>
        </security>
        <!-- Make sure error responses are left untouched -->
        <httpErrors existingResponse="PassThrough" />
    
        <!-- Restart the server if any of these files change -->
        <iisnode watchedFiles="web.config;*.js;browser/*.*" />
      </system.webServer>
    </configuration>