skip to Main Content

I would like to make it so that all requests to my website are redirected to HTTPS with no www. Right now I apparently have both a www and non www version of my site which is supposed to be bad for seo (duplicate content). I also just added an SSL certificate and need to redirect all traffic to the https version of my site. Below is an example of what I am looking to accomplish:

example.com -> https://example.com
www.example.com -> https://example.com
http://www.example.com -> https://example.com
https://www.example.com -> https://example.com

How would I accomplish this within my htaccess file?

2

Answers


  1. These rules need to be at the top of the htaccess file in your document root

    RewriteEngine On
    RewriteCond %{HTTPS} off [OR]
    RewriteCond %{HTTP_HOST} !example.com$ [NC]
    RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
    
    Login or Signup to reply.
  2. try this:

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    RewriteCond %{HTTPS_HOST} ^www.(.*)$ [NC]
    RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
    </IfModule>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search