.htaccess 🔒 HTTP to HTTPS redirections (with or without WWW)

Share on twitter
Share on linkedin
Share on facebook
Share on whatsapp
Share on email
Share on twitter
Share on linkedin
Share on facebook
Share on whatsapp
Share on email

Let's see how to force URLs of your sites to be in HTTPS and to be prefixed or not by WWW.

Redirecting links with WWW to links without WWW

Added to file .htaccess located at the root of your site, the code below redirects URLs of the form www.mon-domaine.fr/chemin/ to https://mon-domaine/chemin/.

# Force non-www 
RewriteEngine On
RewriteCond %{HTTP_HOST} !^mon-domaine\.fr$ [NC]
RewriteRule ^(.*)$ https://mon-domaine.fr/$1 [R=301,L]

Redirecting links without WWW to links with WWW

Added to file .htaccess located at the root of your site, the code below redirects URLs of the form https://mon-domaine/chemin/ to www.mon-domaine.fr/chemin/.

# Force www 
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]

Julien .MA Jacob
Developer WordPress
Julien MA Jacob - Developer WordPress
🧙♂️ Passionate about the web and programming with always lots of projects in mind 👨💻 | About page
  Discussion of the article
Subscribe to our newsletter
Notify of
guest
0 Comments
Inline Feedbacks
View all comments