Last Updated on April 21, 2021 by Amit

Redirect http to https htaccess

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 (.*) https://example.com%{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.)?example.com$ [NC]
RewriteRule (.*) https://example.com%{REQUEST_URI} [L,R=301,NE]

This will redirect only a single domain ie www.example.com to its https version. The regex pattern in the RewriteCond  ^(www.)?example.com$ means to match either www.exaple.com or example.com . 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:

http://example.com http://www.example.com https://example.com



How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.