Recently, a friend built a website and installed an SSL certificate, enabling HTTPS encryption for the entire site. However, after completing the configuration, he discovered that the site could still be accessed via HTTP, which meant users could not be forced into HTTPS encryption mode.

Later, that friend asked me to help solve the problem. I referred to some solutions I found through Baidu, but discovered that none of them worked: repeated 301 redirects caused the website to become inaccessible. So I read some documentation on pseudo-static rules and wrote .htaccess redirection 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.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
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.