19 May 2017

Since Google says https gives websites a small ranking benefit and Let's Encrypt gives free certs, more and more sites have been converted. This is a more thurral snippet of https redirection. It has some rules from Drupal's htaccess. This avoids all kind of redirection loops etc... Also redirect from www to non-www, which can be removed or turned into reverse.

Source code viewer
  1. # BEGIN WordPress
  2. <IfModule mod_rewrite.c>
  3. RewriteEngine On
  4.  
  5. # Set "protossl" to "s" if we were accessed via https://. This is used later
  6. # if you enable "www." stripping or enforcement, in order to ensure that
  7. # you don't bounce between http and https.
  8. RewriteRule ^ - [E=protossl]
  9. RewriteCond %{HTTPS} on
  10. RewriteRule ^ - [E=protossl:s]
  11.  
  12. # Make sure Authorization HTTP header is available to PHP
  13. # even when running as CGI or FastCGI.
  14. RewriteRule ^ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
  15.  
  16. # Redirect all users to access the site WITHOUT the 'www.' prefix.
  17. RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
  18. RewriteRule ^ http%{ENV:protossl}://%1%{REQUEST_URI} [L,R=301]
  19. # To redirect all users to access the site WITH the 'www.' prefix.
  20. # RewriteCond %{HTTP_HOST} .
  21. # RewriteCond %{HTTP_HOST} !^www\. [NC]
  22. # RewriteRule ^ http%{ENV:protossl}://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
  23.  
  24. RewriteBase /
  25.  
  26. # Redirect to HTTPS.
  27. RewriteCond %{HTTPS} off
  28. RewriteCond %{HTTP:X-Forwarded-Proto} !https
  29. RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
  30.  
  31. RewriteRule ^index\.php$ - [L]
  32. RewriteCond %{REQUEST_FILENAME} !-f
  33. RewriteCond %{REQUEST_FILENAME} !-d
  34. RewriteRule . /index.php [L]
  35. </IfModule>
  36. # END WordPress
Programming Language: Apache configuration