Redirect domain from http to https with Apache mod-rewite and htaccess | 100% working code examples you can just copy and paste

When we enable SSL on our domain the next important stap we need complete is http “urls redirection to https” .  We need to redirect all our http URLs to our current HTTPS scheme in order to maintain the SEO rank on search engines and to avoid SSL mixed contents warning that often appears in a web browser when a file loads on a mixed http and https connection.
With RewriteRule directive you can easily redirect all your http URLs to https with just one 301 redirection. 

http to https URL redirection using RewriteRule  directive

You can use the following rule in your server.config or htaccess file to redirect your non secure http URLs to secure Https version on your server.
This is a 100% working rule so you can just copy and paste it to your server.

RewriteEngine on

RewriteCond %{HTTPS} off
RewriteRule (.*){REQUEST_URI} [NE,L,R=301]

The rule above will redirect


The rule above is a basic version of RewriteRule to enforce https on all domains pointing to your document root. 
If you want to redirect a particular domain to https then you may use the following:

RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(www.)?$ [NC]
RewriteRule (.*){REQUEST_URI} [L,R=301,NE]

This will redirect only a single domain ie to its https version. The regex pattern in the RewriteCond  ^(www.)?$ means to match either or . So this applies to both www and the naked version of your domain.

Redirect domain to https and www

You can redirect your domain to https with www  with the following rule :

RewriteEngine on
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www. [NC]
RewriteCond %{HTTP_HOST} ^(www.)?(.+)$
RewriteRule (.*) https://www.%2%{REQUEST_URI} [NE,L,R=301]

The combination of “OR” and “AND” rewrite conditions makes it possible to redirect all versions of http to https://www .
This will redirect all versions of your http and https to https://www
for example:


Leave a Reply

Your email address will not be published. Required fields are marked *