Midrub is unique script which requires an exact url. It’s not professionally to have a website available with www and without, with https and without. For this problem we have htaccess, which helps us to redirect the visitors from www to non www, from http to https and viceversa.

In this article i will provide several examples how to redirect our website to the correct url. All examples works.

First example is to redirect www to non www with https:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

The second example is to redirect www to non www with http:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

The third example is to redirect http to https with www:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.domain.com/$1 [R,L]

The fourth example is to redirect http to https without www:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://domain.com/$1 [R,L]

The fifth example is to redirect https to http:

RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

It’s very simple to get the correct url.