Server Redirects


These lines need to be placed in the .htaccess file just below the “RewriteEngine On” line

Redirect http to https

# Redirect everything to HTTPS
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Permanent redirect

Redirect permanent /

Redirect www to non-www

# Redirect www to non-www
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [L,R=301]

Redirect non-www to www

# Redirect non-www to www
RewriteCond %{HTTP_HOST} ^(?!www\.)(.+) [NC]
RewriteRule ^(.*) http://www.%1/$1 [L,R=301]



This rule should be placed inside <rules>...</rules> in web.config:

<rule name="HTTP to HTTPS redirect" stopProcessing="true">
  <match url="(.*)" />
    <add input="{HTTPS}" pattern="off" ignoreCase="true" />
  <action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}/{R:1}" />

Non-www to www

This rule should be placed inside <rules>...</rules> in web.config:

<rule name="CanonicalHostNameRule">
  <match url="(.*)"/>
    <add input="{HTTP_HOST}" pattern="^example\.com$" negate="false"/>
  <action type="Redirect" url="https://www.{HTTP_HOST}/{R:1}"/>

:exclamation: Don’t forget to replace with your own domain.