Skip to content

Instantly share code, notes, and snippets.

@danieldogeanu
Last active July 31, 2024 17:22
Show Gist options
  • 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

If this was useful, you can buy me a coffee here. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment