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



I would like to redirect my domain from http to https.

But when thinking about duplicatie content, a common issue is the trailing slash. For this reason I would like to implement a 301 redirection to the page with the trailing slash as a first step. The second step should be the redirection from http to https regardless wether the URL contains a www or not.

So here is what I implemented so far:

RewriteEngine On


Redirect 301 “without Trailing Slash” to “with Trailing Slash”


RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(.)/$
RewriteCond %{REQUEST_FILENAME} !.(gif|jpg|png|jpeg|css|js)$ [NC]
RewriteRule ^(.
)$ http://%{HTTP_HOST}/$1/ [L,R=301]


Redirect http 301 to https


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

But when I check my redirection with a Redirect Checker Tool like I get the result:

Too many redirects. Please try to reduce your number of redirects… Actually you use 19 Redirects…

In addition, the redirection from https:// to https://www does not work.

What am I doing wrong? Thanks in advance!


Announcing the Top Users for the month of December (2017)


I am from Customer Success team of Cloudways. We have added two separate rewrite rule to redirect from non-www to www and HTTP to https. It will always open https with www regardless of which combination you try.

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]

Your trailing slash rules are working as it was before.



Thank you, it worked perfectly for me too.



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.