10

I've done this many times before, and yet I'm not managing to do this again for some unknown reason. Tried endless solutions. Everything I've tried results in an endless redirect loop.

What is the best option for doing this?

Update 1 Here are a few of the things tried;

RewriteCond %{HTTP_HOST} ^website\.co\.uk [NC]
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.website.co.uk/$1 [R,L]

And;

RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.website.co.uk$1 [R,L]

Tested in different browsers to prevent caching issues. Still nothing, just getting continual redirect loop.

  • 1
    Try tracing the redirect: redirectdetective.com – jdm2112 Aug 22 '16 at 21:45
  • Tested that (updated question with additional info) - I've even disabled Google Chrome cache to avoid issues like this, stackoverflow.com/questions/5690269/… – Michael Cropper Aug 22 '16 at 21:45
  • Have you tried the other way arround? RewriteCond %{SERVER_PORT} !^433$ – David Aug 22 '16 at 22:10
  • With the answer I gave you in your previous question, did that not fix all of your HTTPS issues? Or are you doing to this to reinforce HTTPS across your website? – Ethan O'Sullivan Aug 22 '16 at 22:50
  • It did for that question yes thanks. I'm wanting to tidy everything up so that if anyone accesses the http version of the site they are 301 redirected to https. – Michael Cropper Aug 23 '16 at 8:48
15

I see, when you enter a link to your page other than your home, example:

  • http://www.michaelcropper.co.uk/contact-me
  • www.michaelcropper.co.uk/contact-me
  • michaelcropper.co.uk/contact-me

If https:// is not in the prefix, the HTTP link loads instead. Add the following into your .htaccess in between the <IfModule mod_rewrite.c> tag:

RewriteCond %{HTTPS} !=on
RewriteRule ^(.*) https://%{SERVER_NAME}/$1 [R,L]

If there were no additional modifications done to your .htaccess, it should look like the following:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# Rewrite HTTP to HTTPS
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*) https://%{SERVER_NAME}/$1 [R,L]
</IfModule>
# END WordPress

Let me know how it goes.

| improve this answer | |
  • Thanks @EthanJinksO'Sullivan this worked. Strangely too, I found another setup where I had this working which was very similar to the original setup I tested which also worked this morning RewriteCond %{SERVER_PORT} 80 RewriteRule ^(.*)$ https://www.michaelcropper.co.uk/$1 [R=301,L] - Based on this I'd have to assume that something somewhere was being cached. Who knows for sure. – Michael Cropper Aug 24 '16 at 9:39
  • 1
    I had to rewrite the code after the RewriteBase \. – Harish ST Oct 24 '18 at 5:01
  • Did not work for me - https rules seems to need to come before, in order to work with inner pages too. – tishma Nov 20 '18 at 13:12
  • 5
    As the other commenters said: You have to insert the two lines right after RewriteBase /. In the position shown in the answer, it will only work for the frontpage. – tanius Sep 24 '19 at 12:06
6

Add code within a .htaccess file

 <IfModule mod_rewrite.c>
   RewriteEngine On
   RewriteBase /

   RewriteCond %{HTTPS} !=on
   RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

   # BEGIN WordPress
   RewriteRule ^index\.php$ - [L]
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteRule . /index.php [L]
 </IfModule>
| improve this answer | |
3

You can set header in .htaccess

<IfModule mod_headers.c>
    Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains" env=HTTPS
</IfModule>
| improve this answer | |
  • This helped me, while none of the above worked. – Clain Dsilva Jan 7 at 2:59
  • +1 This has the benefit that it's less likely to cause conflict with existing mod_rewrite.c rules – Dave A-W Apr 27 at 4:23
2

try this in your .htaccess:

RewriteCond %{HTTP:X-Forwarded-Proto} !=https
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
</IfModule>
# END WordPress
| improve this answer | |
0

Just Add this in your .htaccess file

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress
| improve this answer | |
  • some explanation why this code would solve OP's question is always nice – cjbj May 28 '18 at 10:40
0

If you are using W3 Total Cache the answer from @Ethan Jinks O'Sullivan will only work if you place the redirect before the W3 Total Cache mods in .htaccess as shown below

#BEGIN Rewrite HTTP to HTTPS
<IfModule mod_rewrite.c>
  RewriteCond %{HTTPS} !=on
  RewriteRule ^(.*) https://%{SERVER_NAME}/$1 [R,L]
</IfModule>
#END Rewrite HTTP to HTTPS

# BEGIN GZIP COMPRESSION
...
# END GZIP COMPRESSION

# BEGIN DEFLATE COMPRESSION
...
# END DEFLATE COMPRESSION

# BEGIN W3TC Browser Cache
...
# END W3TC Browser Cache

# BEGIN W3TC Page Cache core
...
# END W3TC Page Cache core

# BEGIN WordPress
<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteRule ^index\.php$ - [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule . /index.php [L]
</IfModule>
# END WordPress
| improve this answer | |
0

Toke me 10 minutes to fix the issue

Add a conf file for your domain.

Go to centos 7 at /etc/httpd/conf.d/, then create a conf file called anything.conf.

Put the following code on it:

<VirtualHost *:80>
    ServerName domain.com
    ServerAlias www.domain.com
    ServerAdmin webmaster@domain.com
    DocumentRoot /var/www/html

    <Directory /var/www/html>
        Options -Indexes +FollowSymLinks
        AllowOverride All
    </Directory>

    ErrorLog /var/log/httpd/example.com-error.log
    CustomLog /var/log/httpd/example.com-access.log combined
</VirtualHost>

Replace example.com with your domain.
Then add the https rewrite code on .htaccess its will work. :)

| improve this answer | |
0

If you don't want to change anything on .htaccess you may try this out

add_action('template_redirect', 'redirect_core', 50);
add_action('init', 'redirect_core', 50);
add_action('wp_loaded', 'redirect_core', 50);
function redirect_core(){
  if (!is_ssl()) {
    wp_redirect('https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], 301);
    exit();
  }
}

Or try this plugin: HTTPS Redirect

| improve this answer | |
-1
#BEGIN Rewrite HTTP to HTTPS
<IfModule mod_rewrite.c>
  RewriteCond %{HTTPS} !=on
  RewriteRule ^(.*) https://%{SERVER_NAME}/$1 [R,L]
</IfModule>
#END Rewrite HTTP to HTTPS

# BEGIN WordPress
<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteRule ^index\.php$ - [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule . /index.php [L]
</IfModule>
# END WordPress
| improve this answer | |
  • 1
    How is your answer different from @ChrisSprague? – nmr Aug 13 '19 at 10:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.