Using .htaccess to Force Apache to Redirect Access to HTTPS

9,858 0

Recently, a friend made a website and installed an SSL certificate to enable full-site HTTPS encryption. However, after completing the configuration, they discovered that HTTP could still be used to access the site, which meant users could not be forced into HTTPS encrypted mode.

Later, that friend asked me to help solve the problem. I referred to some solutions found through Baidu, but found that none of them worked, as they caused repeated 301 redirects and made the website inaccessible. So I read some documentation on pseudo-static configuration and then wrote .htaccess redirect rules to force HTTP requests to access the website via HTTPS.

Important note: The code must be placed at the very beginning of the .htaccess file to ensure redirect priority. If it duplicates existing rules, you only need to write lines 4 and 5.

Plain Text
<IfModule mod_rewrite.c>RewriteEngine OnRewriteBase /RewriteCond %{SERVER_PORT} !^443$RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R=301]</IfModule>

Save the above code to .htaccess. If it does not work, use the Notepad++ editor to save the file.

Comments

(0)

No comments yet. Start the conversation.

Leave a comment