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

Share on email
Share on twitter
Share on linkedin
Share on facebook
Article .htaccess - redirection https www no www by @wpr0ck
Article .htaccess - redirection https www no www by @wpr0ck
Article .htaccess - redirection https www no www by @wpr0ck
Article .htaccess - redirection https www no www by @wpr0ck
Share on twitter
Share on linkedin
Share on facebook
Share on whatsapp
Share on email

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

Redirection of links with WWW to links without WWW

Added to the file .htaccess located at the root of your site, the code below redirects the URLs of the form www.mon-domaine.fr/chemin/ towards 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]

Redirection of links without WWW to links with WWW

Added to the file .htaccess located at the root of your site, the code below redirects the URLs of the form https://mon-domaine/chemin/ towards 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
WordPress Developer
Julien MA JACOB - WordPress Developer
Passionate about the web and programming with always many projects in mind
  Discussion of the article
Subscribe to the newsletter
Notify to
guest
0 Comments
Inline Feedbacks
View all comments