IIS HTTP Redirect too many times error

rudraa gai 0 Reputation points
2023-08-28T13:21:18.45+00:00

I am in process of shifting my website i.e MBAHunt.in to an IIS server but I am having a difficulty setting up an SSL redirection for my website in IIS.  I want to shift it from non-www to www.

I am getting ERR_TOO_MANY_REDIRECTS error. Since I'm not an expert I am not sure how to solve this issue.

I have tried the below code but no luck

<system.webserver>
<rewrite>
<rules>
<rule name=”Redirect to WWW” stopprocessing=”true”>
<match url=”.*”></match>
<conditions>
<add input=”{HTTP_HOST}” pattern=”^yourdomainname.com$”></add>
</conditions>
<action type=”Redirect” url=”http://www.yourdomainname.com/{R:0}” redirecttype=”Permanent”>
</action></rule>

<rule name=”Default Document” stopprocessing=”true”>
<match url=”(.*?)/?default.aspx$”>
<action type=”Redirect” url=”{R:1}/”>
</action></match></rule>
</rules>
</rewrite>
</system.webserver>

Please anyone help me solve this issue? What could be the problem?

Thanks in advance.

Windows for business Windows Server User experience Other
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Limitless Technology 44,746 Reputation points
    2023-08-29T09:33:56.3133333+00:00
    Hello Rudraa,
    
    Thank you for your question and for reaching out with your question today.
    
    I've had a look at your code snippet and there might be a few issues that could potentially cause the redirection loop you're experiencing. Here's an altered version of the code with explanations:
    
    <system.webServer>
      <rewrite>
        <rules>
          <!-- Redirect to www with HTTPS -->
          <rule name="Redirect to www" stopProcessing="true">
            <match url=".*" />
            <conditions>
              <!-- Check if the host is NOT www.yourdomainname.com -->
              <add input="{HTTP_HOST}" pattern="^www\.yourdomainname\.com$" negate="true" />
            </conditions>
            <!-- Redirect to https://www.yourdomainname.com -->
            <action type="Redirect" url="https://www.yourdomainname.com/{R:0}" redirectType="Permanent" />
          </rule>
        </rules>
      </rewrite>
    </system.webServer>
    
    Please note the following:
    
    1. In the <add> condition, the pattern should be ^www\.yourdomainname\.com$ with the \ to escape the dots. This condition checks if the host is NOT www.yourdomainname.com.
    
    2., The redirection URL starts with https://, indicating that the redirection should use HTTPS.
    
    3. The negate="true" attribute ensures that the rule doesn't trigger if the host is already www.your domain name. com, preventing a redirection loop.
    
    If the reply was helpful, please don’t forget to upvote or accept as answer.
    
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.