Skip to content

Instantly share code, notes, and snippets.

@danieldogeanu
Last active October 17, 2019 23:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danieldogeanu/faaa8e3259bbf3451bf96af9749d0297 to your computer and use it in GitHub Desktop.
Save danieldogeanu/faaa8e3259bbf3451bf96af9749d0297 to your computer and use it in GitHub Desktop.
How to redirect a WordPress site to HTTPS via the .htaccess file.

Add the following two lines of code into the .htaccess file:

RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

WARNING: For testing purposes, you might want to remove the [L,R=301] condition from the second line and instead just use [L,R]. R=301 will make your browser PERMANENTLY redirect to the new URL and there's no way you can break out of that if you missconfigure your .htaccess file! You might get around the mistake, but your users WON'T!

Your final .htaccess should look like this:

# 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]
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R]
</IfModule>
# END WordPress
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment