If you've installed an SSL certificate on your website, it's essential to redirect all HTTP traffic to HTTPS to ensure secure connections. This not only protects your visitors' data but also boosts your SEO rankings and builds trust. The easiest and most effective way to enforce HTTPS is by modifying your website’s .htaccess file.
Below are simple code snippets you can add to your .htaccess file to redirect all traffic—or just specific folders—from HTTP to HTTPS.
Redirect Entire Domain from http to https
To redirect all traffic from HTTP to HTTPS for your entire domain, add the following lines to your .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
Redirect a Specific Folder from http to https
If you only want to redirect traffic for a specific folder to HTTPS, use this:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} folder
RewriteRule ^(.*)$ https://www.example.com/folder/$1 [R,L]
Note:
- Replace example.com with your actual domain name.
- Replace the folder with the specific folder name you want to secure.
Conclusion:
Redirecting traffic from HTTP to HTTPS using .htaccess is a quick and effective way to secure your site and improve user trust. Whether you're securing your entire domain or just specific folders, these simple rules ensure your visitors always connect via a secure channel. Don’t forget to test your redirects after updating the .htaccess file to confirm everything works smoothly.