Redirect https to non-www and http to www
Asked Answered
C

2

7

I need a combination of redirects to achieve the following:

To redirect http://example.com to http://www.example.com, while redirecting https://www.example.com to https://example.com.

I would like to force the www prefix do the domain name when the site is accessed over http.

However the SSL certificate only works without the www.

When accessed over https, I don't want the domain name to have the www prefix.

Chromaticness answered 23/5, 2012 at 17:58 Comment(3)
Why on earth would you want that?Unbreathed
Because the ssl certificate is setup for just "mysite.com"Chromaticness
Then, why not also redirect http traffic to the none-www version?Unbreathed
H
13

The redirection from https://www.mysite.com to https://mysite.com can only happen after the client has made an initial request to https://www.mysite.com.

For this initial connection to work, the server at https://www.mysite.com must have a certificate valid for www.mysite.com, otherwise, this connection won't even happen (and the server won't send a redirection response).

If you still want a redirection, on the same server, your server must present a certificate that is valid for the host names you want to serve. You should get a certificate with two Subject Alternative Name DNS entries: mysite.com and www.mysite.com; this will allow you to serve both hosts with the same certificate (and then use the rewrite rules if needed).

(You could also use Server Name Indication with two distinct certificates, if you expect the clients to support it, but that's usually for completely different host names.)

It's quite common for CAs to issue certificates that are valid for both mysite.com and www.mysite.com when you apply for one of the other, sometimes without an extra fee.

Holtz answered 23/5, 2012 at 18:57 Comment(0)
I
0

Put this in your .htaccess file in your non-HTTPS document root:

RewriteCond %{HTTP_HOST} ^mysite.com$
RewriteRule ^(.*)$ http://www.mysite.com/$1 [R]

And this in your HTTPS document root:

RewriteCond %{HTTPS} =on
RewriteCond %{HTTP_HOST} ^www.mysite.com$
RewriteRule ^(.*)$ https://mysite.com/$1 [R]
Insula answered 23/5, 2012 at 18:14 Comment(3)
Neither is working? Are you sure your .htaccess files are being read?Insula
After further testing, yes, the first is working. http://mysite.com is redirected to http://www.mysite.com But https://www.mysite.com is not redirected to https://mysite.comChromaticness
Further testing... I added both of these, just realized https is redirecting to http.Chromaticness

© 2022 - 2024 — McMap. All rights reserved.