Insert the standard IIS URL Rewrite block into your configuration file. This code detects incoming port 80 connections and uses a permanent 301 redirect to forward the host name and URL path to port 443.
How to implement the code in web.config to redirect http to https
To automatically forward website visitors from an insecure connection to a secure one, you must insert a specific XML rewrite rule into the website root configuration file.
[ASP.NET - HTTPS Redirect Rule]

The Required web.config Code
Paste this code block directly into your web.config file. It must sit inside the <system.webServer>
Copy
Search Site
Search Google
configuration tags.
CFFCS | CarrzSynEdit: | ASP.NET (VB/C#)
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="Redirect HTTP to HTTPS" 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>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

[Technical Breakdown of the Code Block]
  • <rule name="..." stopProcessing="true">
    Copy
    Search Site
    Search Google
    : Names the action for the IIS console and stops the server from reading lower rules once this redirect triggers.
  • <match url="(.*)" />
    Copy
    Search Site
    Search Google
    : Uses a wildcard to evaluate every single incoming URL path request.
  • <add input="{HTTPS}" pattern="off" ... />
    Copy
    Search Site
    Search Google
    : Checks the server's encryption state. If the traffic is not encrypted, the pattern matches and the rule proceeds.
  • url="https://{HTTP_HOST}/{R:1}"
    Copy
    Search Site
    Search Google
    : Rebuilds the destination address using your exact domain name {HTTP_HOST}
    Copy
    Search Site
    Search Google
    and original page path {R:1}
    Copy
    Search Site
    Search Google
    .
  • redirectType="Permanent"
    Copy
    Search Site
    Search Google
    : Returns an HTTP 301 status code to the browser to establish the secure URL as the official location.
[Necessary Server Requirements]
The configuration code will only execute if your server meets these parameters:
  1. URL Rewrite Installed: Your Windows Server must have the official IIS URL Rewrite Module extension active. Without it, this code causes an HTTP 500 error.
  2. Port 443 Binding: Your SSL/TLS certificate must be bound to the website inside IIS Manager on port 443.
Googlebot
Using a web.config
Copy
Search Site
Search Google
301 permanent redirect is fully approved by Google and aligns perfectly with its "HTTPS-first" indexing guidelines. The redirectType="Permanent"
Copy
Search Site
Search Google
action explicitly signals Googlebot that your site has moved securely, consolidating your duplicate HTTP and HTTPS pages into a single canonical version while passing 100% of your existing PageRank (ranking power) to the secure URL. Furthermore, because the code utilizes the {R:1}
Copy
Search Site
Search Google
token, Googlebot is seamlessly redirected to the exact deep-linked subpage it intended to crawl rather than being dropped at the homepage, preventing indexing errors or search visibility loss.