How to Redirect HTTP to HTTPS using htaccess
To redirect a website from HTTP to HTTPS, you can use the following code in .htaccess file
1 2 3 |
RewriteEngine On RewriteCond %{HTTPS} !=on RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE] |
If you want to redirect HTTP to HTTPS and keep SSL verification pages on HTTP, this is needed on Cpanel servers.
1 2 3 4 5 6 |
<IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{HTTPS} !=on [NC] RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/ RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L] </IfModule> |
If you wish to add redirect in Apache VirtualHost, use the following VirtualHost entry.
1 2 3 4 |
<VirtualHost *:80> ServerName www.yourdomain.com Redirect permanent / https://www.yourdomain.com/ </VirtualHost> |
See htaccess