how to redirect website to main url?

Sudesh Sharma 176 Reputation points
2021-11-18T06:18:09.633+00:00

Hey Guys,

I have created React nodejs app and hosted the app on the azure VM IIS webserver. I have defined below set of rules in web.config file

<configuration>
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="" />
</customHeaders>
</httpProtocol>
<iisnode node_env="%node_env%" nodeProcessCountPerApplication="1" maxConcurrentRequestsPerProcess="1024" maxNamedPipeConnectionRetry="100" namedPipeConnectionRetryDelay="250" maxNamedPipeConnectionPoolSize="512" maxNamedPipePooledConnectionAge="30000" asyncCompletionThreadCount="0" initialRequestBufferSize="4096" maxRequestBufferSize="65536" uncFileChangesPollingInterval="5000" gracefulShutdownTimeout="60000" loggingEnabled="true" logDirectory="iisnode" debuggingEnabled="true" debugHeaderEnabled="false" debuggerPortRange="5058-6058" debuggerPathSegment="debug" maxLogFileSizeInKB="128" maxTotalLogFileSizeInKB="1024" maxLogFiles="20" devErrorsEnabled="true" flushResponse="false" enableXFF="false" promoteServerVars="" configOverrides="iisnode.yml" watchedFiles="web.config;
.js" nodeProcessCommandLine="C:\Program Files\nodejs\node.exe" />
<handlers>
<add name="iisnode" path="server.js" verb="*" modules="iisnode" />
</handlers>

<rewrite>
  <rules>


    <rule name="DynamicContent">
                     <conditions>
                          <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True"/>
                     </conditions>
                     <action type="Rewrite" url="server.js"/>
                </rule>

              <rule name="Redirect HTTP to HTTPS" stopProcessing="true">
                <match url="(.*)" />
                <conditions>
                    <add input="{HTTPS}" pattern="off" />
                </conditions>
                <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
            </rule>
            <rule name="non-wwwtowww" stopProcessing="true">
                <match url="(.*)" />
                <conditions>
                    <add input="{HTTP_HOST}" pattern="^xyz\.com$" />
                </conditions>
                <action type="Redirect" url="https://xyz.com/{R:1}" />
            </rule>
  </rules>
   <outboundRules>
            <rule name="Add the STS header in HTTPS responses">
                <match serverVariable="RESPONSE_Strict_Transport_Security" pattern=".*" />
                <conditions>
                </conditions>
                <action type="Rewrite" value="max-age=31536000" />
            </rule>
        </outboundRules>
</rewrite> 

</system.webServer>
</configuration>

whenever I have tried to open my website directly from the browser its redirects to https://www.xyz.com/server.js. I want to redirect my website to https://www.xyz.com. I have defined a rewrite rule for server.js (It's important for node js applications). Please help me guys to resolve this issue.

Thanks, Guys in advance.

Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
7,129 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Ravi Kanth Koppala 3,231 Reputation points Microsoft Employee
    2021-11-23T10:37:30.893+00:00

    @Sudesh Sharma ,

    I see a negate pattern in the web.config (1st rule). A pattern can be negated by using the negate attribute of the <match> element. The rule action is performed only if the current URL does not match the specified pattern when this attribute is used. This can be used to specify a condition that checks if the requested URL is NOT a file - :
    <add input="{REQUEST_FILENAME}" matchType="isFile" negate="true">

    I feel, this condition is making the app redirect to server.js. Do you require the above attribute in your web.config? If not, can you comment and check if the URL redirection is working as expected.

    I hope that helps.

    Please 'Accept as answer' if the provided information is helpful to help others in the community looking for help on similar topics.

    1 person found this answer helpful.
    0 comments No comments