.htaccess force SSL redirect of TLD or Subdomain

If you want to apply a force SSL redirects of top-level domains with www only, Use the following code:

RewriteEngine On 
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)$  [NC]
RewriteRule (.*) https://www.%1/$1 [R=301,L]

TEST case are as follows
http://example.com Should redirect to https://www.example.com

http://www.example.com Should redirect to https://www.example.com

https://example.com Should redirect to https://www.example.com

https://www.example.com Should redirect to https://www.example.com

If you want to apply a force SSL redirect to a subdomain domain using .htaccess, Use the following code:

RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^blog\.example\.com [NC]
RewriteRule .? https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Replace blog with your sub-domain and example with your TLD

You can also use http://www.redirect-checker.org/ to check the redirection.

Know how to use .htaccess to set custom 404 page https://blog.certificationskart.com/php/using-htaccess-to-set-custom-404-page/632/

Leave a Reply

Your email address will not be published. Required fields are marked *