Redirect HTTP to HTTPS Using .htaccess

Nowadays with HTTPS (Hyper Text Transfer Protocol Secure) should be everywhere for the websites and Google has recognized this as a ranking sign to their search engines.
Without SSL certificates all browsers began showing insecure warnings on the websites. When viewing our website without SSL it shows insecure to the visitors. Hence using SSL connection for security convenience. So It is very necessary to redirect from HTTP to HTTPS.
HTTPS, also help for website traffic and there are guidance/directives in the .htaccess file that instruct the server how to respond in certain situations and immediately affects how your website functions.
There are several ways to use this redirection, and Lets here we see the following few options to redirect HTTP to HTTPS.
Add the following code in your .htaccess.
Redirect Based On Apache Server
1 2 3 |
RewriteEngine On RewriteCond %{HTTPS} !=on RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L] |
Redirect Based On Apache Server (Optional)
1 2 3 |
RewriteEngine On RewriteCond %{SERVER_PORT} 80 RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R,L] |
Redirect Based On Port Number
1 2 |
RewriteCond %{SERVER_PORT} ^80$ RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L] |
Redirect Only a Specific Domain
1 2 3 4 |
RewriteEngine On RewriteCond %{HTTP_HOST} ^yourdomain\.com [NC] RewriteCond %{SERVER_PORT} 80 RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R,L] |
Redirect Only a Specific Folder
1 2 3 4 |
RewriteEngine On RewriteCond %{SERVER_PORT} 80 RewriteCond %{REQUEST_URI} folder RewriteRule ^(.*)$ https://www.yourdomain.com/folder/$1 [R,L] |
.htaccessawscloud serverserver
Mraj
Creative Designer & Developer specialist by the spirit and a loving blogger by thoughts. If you have any questions let me drop an email with the article name to the following email id: [email protected]