Articles & Code Snippets


How to force HTTPS using the .htaccess file

How to force HTTPS using the .htaccess file using mod_rewrite.



RewriteEngine On 
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
To force a specific domain to use HTTPS, use the following lines of code in the .htaccess file in your website's root folder:

RewriteEngine On 
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
If you want to force SSL on a specific URL (without www) helpful to avoid duplicate URLs in SEO

RewriteCond %{HTTP_HOST} ^www.example.com
RewriteRule (.*) https://example.ca/$1 [R=301,L]
  
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ https://example.ca/ [R=301,L]
If you want to force SSL on a specific folder you can insert the code below into a .htaccess file placed in that specific folder:

RewriteEngine On 
RewriteCond %{SERVER_PORT} 80 
RewriteCond %{REQUEST_URI} folder 
RewriteRule ^(.*)$ https://www.example.com/folder/$1 [R,L]
web


Archives

Just feel like saying Hi?

  1 (903) NEGRARU phone
  email