Redirecting Http=>Https (Non-Www) + Www Https=> Non-Www Https (No Certificate for Www)

Redirecting Http=>Https (Non-Www) + Www Https=> Non-Www Https (No Certificate for Www)

I have seen many articles on rewrites. All good. But none covering this exact situation. So here is my question: hope you can help. Because I cannot get it to work.

  • we run website on domain.com (non-www)
  • we have ssl set up (so https only)
  • we have 1 certificate for the non-www version (not www version)

When we execute all four test cases, 3 are OK, 1 not

  1. => =>
  2. => => oK
  3. => OK
  4. => ERROR. Certificate warning not safe

Question: Now why is number 4 giving me this error. I would expect the first rule to pick-up and send us to the non-www version. And how do I fix this?

Appreciate any help ;P Sean


This is my currect htaccess

<IfModule mod_rewrite.c>
  RewriteEngine on
  # Redirect www to non-www first 
  # Added based on article 
  RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
  RewriteRule ^(.*)  [R=301,NE,L]

  # Then redirect http to https (if necessary)
  RewriteCond %{HTTPS} !=on
  RewriteRule ^/?(.*)  [R=301,L]
</IfModule>
10

2 Answers

You can use this in your .htaccess:

<IfModule mod_rewrite.c>
  RewriteEngine on
  # Redirect www to non-www first 
  RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
  RewriteRule ^(.*)  [R=301,NE,L]

  # Then redirect http to https (if necessary)
  RewriteCond %{HTTPS} off
  RewriteRule ^  [NE,L,R=301]
</IfModule>

But there's nothing you can do for certificate error.
Read: Redirecting to - without seeing certificate error, possible?

This is not possible to solve only by htaccess or nginx conf.

One needs a certificate to cover both the www and non www version of the domain

So a wildcard cert or a multidomain cert where both www and non www are included as a separate domain

Hope it helps

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Maya Lin-Takahashi
Author

Maya Lin-Takahashi

Maya is a hardware enthusiast who tests and reviews smart home devices, smartphones, wearables, and audio gear. She focuses on practical consumer value and build quality.