Search code examples
.htaccesssslmod-rewritednscloudways

Is it possible to forward https://www.example to https://example?


I have my site at a host and my domain with a registrar. I have a subdomain pointed to my hosted site via an A record.

From the beginning. I am using cloudways hosting. I set up my site. (A wordpress site.) I then went to my registrar and set up an A record to point to my site. It worked. The A record was simply sub.example.c om. I then tested that url and it worked. So I made a second A record for www.sub.example.c om so I could be able to get to my site with or without www. The www worked. So far so good.

Since I had everything working I decided to set up my SSL. CloudWays has Let's Encrypt integration. So ithas the option to set it up for you. I set it up and went to https://sub.example.com. It worked. I then went to https://www.sub.example.com. and got a certificate error. I didn't think about it when I set up the cert, but I used sub.example.com. So it makes sense the site only works without the www.

Since Let's Encrypt doesn't do wildcard certs I need a work around. Is there a way I can forward https://www.mysub to https://mysub? using mod rewrite? Or any other way?


Solution

  • No idea what pedrofb was talking about. You're clearly talking about a single sub domain. Not the top level domain. So ignore the thing about another SSL cert. And I'm sorry for the misdirected anger causing you downvotes. You'd think this community would be more willing to help. The fact that you're being downvoted means your missing something which should simply be explained kindly.

    You're close. If you go to iconfinder .com and look at their cert it is a wild card cert. Which is why there is no certificate error at h ttps://www.iconfinder You'll also notice when you go to www.iconfinder you get https and when you go to iconfinder alone you get https. Their cert covers both h ttps://iconfinder and h ttps://www.iconfinder

    Yours does not.

    Go to letsencrypt. org. Notice how their url always shows h ttps://letsencrypt. org? No go directly to h ttps://www.letsencrypt.org. You get a server error. So what is happening to you is happening to them. That shows there is a limitation with their not being a wildcard cert.

    Now you wanted to know if you could forward. Go to www.letsencrypt.org. It takes you right back to https://letsencrypt.org. Now go to letsencrypt.org. It takes you right back to https://letsencrypt.org.

    So as you can see you can forward. So to accomplish what you want to do. Below is how to do it.

    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^mysub.example.com [NC]
    RewriteCond %{HTTP_HOST} ^www.mysub.example.com [NC]
    RewriteRule ^(.*)$ https://mysub.example.com/$1 [L,R=301,NC]