Redirect http:// to https:// and more



Many thanks! Now it’s working perfectly!



Thank you!
Worked for me as well.


I am having problems with this same issue. I used the text posted by Faizan khan above. Additionally, I need a Rewrite rule for “pretty URLs” and one to redirect from the root directory to the content directory. Here is the text of my .htaccess file:

Options All -Indexes
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^ [NC]
RewriteRule ^(.*)$$1 [L,R=301,NC]
RewriteEngine on
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

# Pretty URLs
RewriteRule ^/?wiki(/.*)?$ %{DOCUMENT_ROOT}/w/index.php [L]

# Redirect / to Main Page
RewriteRule ^/*$ %{DOCUMENT_ROOT}/w/index.php [L]

The problems I am experiencing are:

  • When I direct the browser to it does not redirect to add the www and as a result the browser returns the message Your connection is not secure.
  • When I type the domain name into a redirection checker such as or, they return a large number of redirects, indicating a loop is happening.

Any help is greatly appreciated!




Before answering, I would like to confirm one simple thing -

  • Do you want to keep (HTTPS + www) for all HTTP requests?


Yes, that is what I would like to do.


Try this one

# Force HTTPS + www in one go
RewriteEngine On 
RewriteCond %{HTTP_HOST} ^wikilawschool\.net [NC,OR]
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$$1 [R=301,L]

Note: This rule should be placed in the top of .htaccess file before any other rewrite rule.
Edit: Corrected by adding missing OR flag. Credit: Jeff


Thank you! One question: Should the third line have an [OR] flag like:
RewriteCond %{HTTP_HOST} ^wikilawschool\.net [NC,OR]

Because if I understand it, that line checks to see if www is missing (which by itself we want to trigger the RewriteRule), and then the next line checks to see if https was not used (which by itself should also trigger the RewriteRule).

Edit: added the not

Edit2: removed the space in the flag


You are right. Thanks.

Once done, please make sure to check HTTP header response for all URLs variation


Great, it’s working perfectly. At first I included a space in the flag [NC,OR] and it broke stuff–so don’t do what I did! Once I removed the space everything is good.

On an interesting note: at first after I uploaded the updated .htaccess file, it still was not redirecting properly–everything else was except that. Whenever I entered that address, I got a warning that this domain was not part of the SSL so the browser wouldn’t go there without the www.

I decided to just accept that and make sure to include the domain on the SSL certificate. The minute I regenerated the SSL to include the both domains, it started properly redirecting to Coincidence? Or would the browser’s security prevent the redirect from happening?

Anyway, huge thank you to solving the problem for me!


Glad to know, it successfully resolved.

For enabling HTTPS on www and non-www even for same domain; we install SSL for both variations.

#####Source: KB

I hope, after regenerating SSL certificate, your website is working fine. :slight_smile:



The https redirect is working fine for me, but I’m not able to pass the query string variables to https. Here’s what I’ve been trying, but it’s not working.

RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.)$
RewriteCond %{HTTP_HOST} ^ [NC,OR]
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.
)$$1?%1 [L]


Try this way


No luck. The goal here is to get urls like the one below to forward to https. When I changed to the syntax you suggested above, the domain redirected to https, but the query string parameters were not passed.


Can you please mention domain name (If privacy is concerned, you could inbox also).



The domain is


I can see, It’s redirecting with HTTP code 302, instead of 301 and it is missing query string value. Probably, there is something wrong with your .htaccess rule.

Solution: Keep this rule at the top of WordPress default rewrite rule. And, don’t use any other things for similar purposes. After then, make sure to Purge Varnish Cache.

RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]


Use this tool, it works great.


thanks for sharing that tool :slight_smile:


Thanks for the great post. I am using the following rewrite rule (without spaces). I am getting http redirected to https and non-www redirected to www. However, my problem is I am getting a 302 redirect for “”. It is redirecting to https:// www. Any idea how to get this to a 301 or just not worry about it?

RewriteEngine on
RewriteCond %{HTTP_HOST} ^website .com [NC]
RewriteRule ^(.)$ http://www. website .com/$1 [L,R=301,NC]
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.
)$ https://%{HTTP_HOST}/$1 [R=301,L]


Hi @jwebb189 this is what I do on cloudways that works for me:

RewriteEngine On 
RewriteCond %{HTTP_HOST} !^domain\.com [NC,OR]
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$$1 [R=301,L]

For the last line add www. if you want it to go to

It returns 301 for me and correctly redirects all attempts to

You can see for yourself at the site I am using these rules for at that is on cloudways.