Before I give you the CODE, let me explain a few points:
Point 1:
It's better if you only allow https
links. Mixing http
& https
for the same content breaks the security added by https
. With http
, you can never be sure that your visitors are shown the same page you are providing from your server.
Point 2:
Search engines consider http
& https
sites to be different sites, even when the domain is the same. So if not set up properly, you may get duplicate penalty. So it's recommended to go https
all the way for better SEO as well.
Point 3:
Even with proper .htaccess
CODE, you may get the same error if SSL is not set up properly. So after you change the .htaccess
CODE, it's better to test your SSL setup against standards. You may use tool such as this.
Recommended CODE:
Based on the points above, following is the CODE you may use:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} =off [OR]
RewriteCond %{HTTP_HOST} !^example\.com$
RewriteRule ^(.*)$ "https://example.com/$1" [R=301,L]
# remaining htaccess mod_rewrite CODE for WordPress
</IfModule>
This will do the following example redirects:
http://www.example.com/ → https://example.com/
http://www.example.com/abc → https://example.com/abc
https://www.example.com/ → https://example.com/
https://www.example.com/abc → https://example.com/abc
# Additionally, even if your site is reachable by IP or
# some other domains or subdomains, this CODE will fix that too
http://Your_Server_IP/abc → https://example.com/abc
http://sub-dmoani.example.com/abc → https://example.com/abc
http://www.other-domain.com/abc → https://example.com/abc
Not Recommended CODE:
If for some reason, you don't want to follow the above recommendation & still want to allow both http
and https
, you may use the following CODE (based on this answer):
<IfModule mod_rewrite.c>
RewriteEngine On
# set protocol variable
RewriteCond %{HTTPS} =on
RewriteRule ^(.*)$ - [env=proto:https]
RewriteCond %{HTTPS} =off
RewriteRule ^(.*)$ - [env=proto:http]
RewriteCond %{HTTP_HOST} !^example\.com$
RewriteRule ^(.*)$ "%{ENV:proto}://example.com/$1" [R=301,L]
# remaining htaccess mod_rewrite CODE for WordPress
</IfModule>
This will do the following example redirects:
http://www.example.com/ → http://example.com/
http://www.example.com/abc → http://example.com/abc
https://www.example.com/ → https://example.com/
https://www.example.com/abc → https://example.com/abc
http
requests to go tohttps
?