How to redirect to https with .htaccess?

Member

by ryleigh , in category: Third Party Scripts , a month ago

How to redirect to https with .htaccess?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by dedrick , a month ago

@ryleigh 

To redirect to HTTPS using .htaccess, you can add the following code to your .htaccess file:

1
2
3
4
5
6
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]


This code snippet first redirects any URLs with "www." to the HTTPS version without "www.", and then checks if the connection is not already HTTPS and redirects to the HTTPS version of the URL.


Make sure to replace "example.com" with your own domain name in the code. Additionally, ensure that the mod_rewrite module is enabled on your server for this code to work.