Redirect all HTTP requests to HTTPS automatically using .htaccess and web.config
Redirect HTTP to HTTPS automatically with Linux-based and Windows. Using the following code in your .htaccess (Linux & cPanel) and web.config (Windows) file automatically redirects visitors to the HTTPS version of your site.
Linux & cPanel: Linux-based accounts use .htaccess files to handle redirection.
1 2 3 |
RewriteEngine On RewriteCond %{HTTPS} !on RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} |
{OR}
1 2 3 |
RewriteEngine On RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] |
Windows: Windows-based accounts use web.config files to handle redirection.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<configuration> <system.webServer> <rewrite> <rules> <rule name="HTTP to HTTPS redirect" stopProcessing="true"> <match url="(.*)" /> <conditions> <add input="{HTTPS}" pattern="off" ignoreCase="true" /> </conditions> <action type="Redirect" redirectType="Permanent" url="https://{HTTP_HOST}/{R:1}" /> </rule> </rules> </rewrite> </system.webServer> </configuration> |