user3532637
user3532637

Reputation: 333

Redirect addon domains with/to HTTPS

There is one Domain:

And 3 addon domains:

My goal is to redirect every domain to https://www.maindomain.com

This is what I tried (.htaccess):

RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} (^(?:www\.)?(addondomain|addon-domain)\.com$|^(?:www\.)?addondomain\.net$) [NC]
RewriteRule ^ https://www.maindomain.com%{REQUEST_URI} [R=302,L,NE]

Or this (.htaccess):

RewriteCond %{HTTP_HOST} ^www\.?addondomain\.com [NC]
RewriteRule ^(.*)$ https://www\.maindomain\.com/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^www\.?addon-domain\.com [NC]
RewriteRule ^(.*)$ https://www\.maindomain\.com/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^www\.?addondomain\.net[NC]
RewriteRule ^(.*)$ https://www\.maindomain\.com/$1 [R=301,L]

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.maindomain\.com$ [NC]
RewriteRule ^(.*)$ https://www.maindomain.com/$1 [L,R=301]

This works if for example www.addondomain.com or addondomain.com is entered as URL. But if someone types https://www.addondomain.com the notice "this connection is not secure" appears.

Do I have to apply for a SSL-Certificate for every addon-domain to make it work? Or is there something wrong in my .htaccess file?

Upvotes: 1

Views: 683

Answers (1)

MrWhite
MrWhite

Reputation: 45914

Do I have to apply for a SSL-Certificate for every addon-domain to make it work?

Yes. The SSL handshake (and browser error) occurs at the very start of the request, before your server (and your .htaccess code) is able to process the request.

Note that this doesn't necessarily mean installing a new SSL cert for each domain. You can get multi-domain/SAN certs - which are supported by all modern browsers.

RewriteCond %{HTTP_HOST} ^www\.?addondomain\.com [NC]
RewriteRule ^(.*)$ https://www\.maindomain\.com/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^www\.?addon-domain\.com [NC]
RewriteRule ^(.*)$ https://www\.maindomain\.com/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^www\.?addondomain\.net[NC]
RewriteRule ^(.*)$ https://www\.maindomain\.com/$1 [R=301,L]

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.maindomain\.com$ [NC]
RewriteRule ^(.*)$ https://www.maindomain.com/$1 [L,R=301]

Incidentally, you don't need the first three rule blocks. The last one is sufficient. But remove the NC flag on the negated condition. (You were also missing a space before the flags on the 3rd rule). In other words:

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.maindomain\.com$
RewriteRule (.*) https://www.maindomain.com/$1 [L,R=301]

Upvotes: 1

Related Questions