How to redirect to https with .htaccess?

Member

by ryleigh , in category: Third Party Scripts , 7 months ago

How to redirect to https with .htaccess?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by dedrick , 7 months 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.

Related Threads:

How to always redirect to https using .htaccess?
How to redirect all traffic via .htaccess to https?
How to redirect website url to https:// using .htaccess file?
How to redirect http to https and https://www to https://?
How to redirect https://ip.address to https://domain?
How to redirect https://www to non-www https version in nginx?