HTTP to HTTPS redirection

Hello Friends,
Setting up HTTP to HTTPS redirection is one of painful tasks which we used to get errors like Redirection Loop or Page isn’t Redirecting properly.  Below given is a global code which can be used to redirect a HTTP to HTTPS .  Just add the below code at the top of your .htaccess file:
~~~~
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
~~~~

The following code may help you to do domain specific HTTP to HTTPS redirection.
~~~~~~~~~~~~
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?domain.com$
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.domain.com%{REQUEST_URI} [NC,L,R]
~~~~~~~~~~~~

Similarly For Windows redirection is setup via web.config file. The global web.config code to HTTP to HTTPS redirection is given below:
~~~~~

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <clear />
                <rule name="Redirect to https" stopProcessing="true">
                    <match url="(.*)" />
                    <conditions>
                        <add input="{HTTPS}" pattern="off" ignoreCase="true" />
                    </conditions>
                    <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="false" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

~~~~~

The above codes should work like a charm.

Have a great day !!!! 🙂 😉

Advertisements

2 thoughts on “HTTP to HTTPS redirection”

  1. Hi,

    The following code can help you to redirect www to non-www
    ~~~~
    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
    RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
    ~~~~

    No need to define your domain name in it. If your site is a wordpress site, please consider changing the Site and home URL to non-www.

    Like

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s