Set preferred domain (with or without www) and HTTP to HTTPS

The increasing adoption of web browsers to the display of warnings 'not safe' for websites not encrypted with the protocol HTTPS has introduced some new challenges for web developers and administrators such as the need to consolidate a canonical domain when redirecting HTTP sites to HTTPS, as well as redirecting their domains with www or without www (or vice versa).

http://example.com
http://www.example.com

a

https://example.com
https://www.example.com


Configuration in Apache

Open the .htaccess file using your favorite code editor then copy/paste the following codes

without www / HTTP to HTTPS

Predetermined: https://example.com

RewriteEngine On RewriteCond %{HTTPS} off [OR] RewriteCond %{HTTP_HOST} ^www\. [NC] RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC] RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]

with www / HTTP to HTTPS

Predetermined: https://www.example.com

RewriteEngine On RewriteCond %{HTTPS} off [OR] RewriteCond %{HTTP_HOST} !^www\. [NC] RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC] RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=301]

If you want to understand how the configuration works and want to make more modifications, visit the author's page.

Font: https://simonecarletti.com/blog/2016/08/redirect-domain-http-https-www-apache/

Commentary

Comments

index.pe: Specialist in Front-End, Back-End, SEO, Social Media and UX.
Related Post
Leave a Comment

This page uses cookies.