title: How to use ONLY .htaccess to force HTTPS on all pages on WordPress site hosted on 1and1
body: <p>I have tirelessly tried to force HTTPS to my entire WordPress website after activating my free SSL certificate I received from 1and1 hosting so that all past HTTP links are redirected to HTTPS. I want to force HTTPS by modifying only the .htaccess file in the root of my web folder. I have tried multiple examples I have found on the web including:</p>
  
  <p><a href="http://www.wpbeginner.com/wp-tutorials/how-to-add-ssl-and-https-in-wordpress/" rel="nofollow noreferrer">http://www.wpbeginner.com/wp-tutorials/how-to-add-ssl-and-https-in-wordpress/</a></p>
  
  <pre><code>&lt;IfModule mod_rewrite.c&gt;
  RewriteEngine On
  RewriteCond %{SERVER_PORT} 80 
  RewriteRule ^(.*)$ https://www.yoursite.com/$1 [R,L]
  &lt;/IfModule&gt;
  </code></pre>
  
  <p><a href="https://www.1and1.com/digitalguide/hosting/technical-matters/the-ten-best-htaccess-tricks/" rel="nofollow noreferrer">https://www.1and1.com/digitalguide/hosting/technical-matters/the-ten-best-htaccess-tricks/</a></p>
  
  <pre><code>&lt;IfModule mod_rewrite.c&gt;
  # activate HTTPS
  RewriteEngine On
  RewirteCond %{Server_Port} !=443
  RewriteRule ^(.*)$ http://yourdomain.tld/$1 [R=301, L]
  &lt;/IfModule&gt;
  </code></pre>
  
  <p>After trying all these methods of modifying the .htaccess file, none of them worked. I finally came across this <a href="https://stackoverflow.com/questions/4398951/force-ssl-https-using-htaccess-and-mod-rewrite">Stack Overflow question</a> and put the PHP block of code in the functions.php file in my current theme folder. Here is the code:</p>
  
  <pre><code>if (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] !== 'on') {
      if(!headers_sent()) {
          header("Status: 301 Moved Permanently");
          header(sprintf(
              'Location: https://%s%s',
              $_SERVER['HTTP_HOST'],
              $_SERVER['REQUEST_URI']
          ));
          exit();
      }
  }
  </code></pre>
  
  <p>This PHP code accomplished my goal of forcing all links to HTTPS. However, I desire to keep my code clean and leave it all in the .htaccess file for better organization and so I don't have to remember to add the PHP code to the functions.php file every time I change the theme of my WordPress website.</p>
  
  <p><strong>Could anyone please explain how to accomplish forcing HTTPS by ONLY modifying the .htaccess file or explain where I may have gone wrong?</strong></p>
  
  <p>The only active block of code in my .htaccess file is this:</p>
  
  <pre><code># BEGIN WordPress
  &lt;IfModule mod_rewrite.c&gt;
  RewriteEngine On
  RewriteBase /
  RewriteRule ^index\.php$ - [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule . /index.php [L]
  &lt;/IfModule&gt;
  
  # END WordPress
  
  AddHandler x-mapp-php5.5  .php
  </code></pre>
  
  <p>And I already changed my WordPress settings of "WordPress Address (URL)" and "Site Address (URL)" to "<a href="https://example.com/" rel="nofollow noreferrer">https://example.com/</a>".</p>