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


#4

Hi,

Many thanks! Now it’s working perfectly!

Regards


#5

Thank you!
Worked for me as well.


#6

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} ^wikilawschool.net [NC]
RewriteRule ^(.*)$ http://www.wikilawschool.net/$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]
</IfModule>

The problems I am experiencing are:

  • When I direct the browser to https://wikilawschool.net 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 http://redirectdetective.com/ or http://redirectcheck.com/index.php, they return a large number of redirects, indicating a loop is happening.

Any help is greatly appreciated!


#7

Hello,

@HandwovenBox

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

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

#8

Yes, that is what I would like to do.


#9

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 ^(.*)$ https://www.wikilawschool.net/$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


#10

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


#11

You are right. Thanks.

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


#12

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 https://wikilawschool.net 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 wikilawschool.net on the SSL certificate. The minute I regenerated the SSL to include the both domains, it started properly redirecting https://wikilawschool.net to https://www.wikilawschool.net. Coincidence? Or would the browser’s security prevent the redirect from happening?

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


#13

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:

Thanks


#14

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} ^content.ad [NC,OR]
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.
)$ https://www.content.ad/$1?%1 [L]


#15

Try this way


#16

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.

content.ad/?utm_source=affiliaxe


#17

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

Thanks


#18

The domain is content.ad


#19

@systems
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]

#20

Use this tool, it works great.

https://varvy.com/tools/redirects/


#21

thanks for sharing that tool :slight_smile:


#22

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 “https://website.com”. 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]


#23

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 ^(.*)$ https://domain.com/$1 [R=301,L]

For the last line add www. if you want it to go to https://www.domain.com

It returns 301 for me and correctly redirects all attempts to https://domain.com

You can see for yourself at the site I am using these rules for at https://myipiswhat.com that is on cloudways.