How to permanently redirect `http://` and `www.` urls to `https://`?

by herminia_bruen , in category: Third Party Scripts , 2 months ago

How to permanently redirect `http://` and `www.` urls to `https://`?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by ryleigh , 2 months ago

@herminia_bruen 

To permanently redirect http:// and www. URLs to https://, you can set up a 301 redirect in your .htaccess file if you are using an Apache server. Here's how you can do it:

  1. Open your website's .htaccess file. You can usually find it in the root directory of your website.
  2. Add the following lines at the beginning of your .htaccess file:
1
2
3
4
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www. [NC]
RewriteRule ^ https://example.com%{REQUEST_URI} [L,NE,R=301]


Replace example.com with your actual domain name.

  1. Save the changes to your .htaccess file.


This code will check if the request is using http:// or if the request is coming in with www. and then redirect it to https:// without the www.. The R=301 flag ensures that the redirect is permanent.


After adding this code to your .htaccess file, all URLs with http:// or www. will be permanently redirected to https:// on your website.