1

I want to rewrite http://, http://www. https://www. and nonexistent subdomain variations to https://domain.com. A nonexistent subdomain variation might be http://abc.domain.com and https://abc.domain.com. To complicate matters further, I want to keep trailing URLs, and prevent redirecting existing subdomains. AND, some existing subdomains use http while others use https.

This is what I have so far:

#don't redirect existing subdomains example1 or example2
RewriteCond %{HTTP_HOST} ^((example1|example2)\.)?domain\.com$
RewriteRule .* - [L]

##redirect http, wildcard subdomains, and www. with appended trailing URLs
RewriteCond %{HTTP_HOST} . [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [NC]
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://domain.com/$1 [R=301,L]

Thanks in advance.

Dustin
  • 75
  • 7
  • If a sub domain is non existing it won't resolve by DNS server and your server won't get any request for it. – anubhava Jun 14 '14 at 17:36
  • @anubhava Currently on my site, any non-existent subdomain entered resolves to the top level website. So, `domain.com` and `fake.domain.com` serve the same content. Bluehost said it was not a DNS issue and that I need to script a fix in htaccess. – Dustin Jun 16 '14 at 14:21
  • If `fake.domain.com` is resolving to `domain.com `then how to identify `fake.domain.com` as non-existent subdomain? – anubhava Jun 16 '14 at 14:23
  • @anubhava I was trying to redirect any/all sub domains to domain.com except existing sub domains -- because I can identify/specify existing sub domains, but not nonexistent ones.. – Dustin Jun 16 '14 at 15:38
  • ok check my answer below for that. – anubhava Jun 16 '14 at 15:41
  • @anubhava That sure looks like it should work, but it seemed to cause a loop. I still got the loop after I commented out all other rewrite and refreshed cache. I also tried removing the first redirect in my example and replacing the condition `RewriteCond %{HTTP_HOST} . [OR]` with your condition, but, no luck. – Dustin Jun 16 '14 at 17:02
  • @anubhava Sorry, after I fixed a typo (of mine) it worked for http but didn't rewrite `https://fake.domain.com`. – Dustin Jun 16 '14 at 17:12
  • ok check my updated answer again. – anubhava Jun 16 '14 at 17:15
  • Oh, ha. I didn't realize you changed your answer. Your change (the ?) and my typo were the same thing. So, it works, but not with `https://fake.domain.com`. – Dustin Jun 16 '14 at 17:15
  • Wow! It works. You're a genius. Thank you. I checked/accepted the answer, but apparently I can't upvote it until I have 15 reputation points. So, I'll double back soon to do so. – Dustin Jun 16 '14 at 17:37

1 Answers1

2

Here is how you can redirect nonexistent subdomains to https://domain.com:

RewriteCond %{HTTP_HOST} !^domain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^(valid1|valid2|valid3)\.domain\.com$
RewriteRule ^ https://domain.com%{REQUEST_URI} [R=301,L,NE]

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule ^ https://domain.com%{REQUEST_URI} [R=301,L,NE]
anubhava
  • 761,203
  • 64
  • 569
  • 643