2
votes

Hello!

I'm trying to set up my .htaccess file for wildcard subdomains, but I really have no clue how to do that.

I have "domain2" pointing to "domain1" as an alias, which is working perfectly, this is the code I'm using:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^(www)\.(.*)\.(.*)\.(.*) [NC]
RewriteRule ^(.*)$ http://%2.%3.%4/$1 [R=301,QSA,L]

RewriteCond %{HTTP_HOST} ^(.*\.?)domain2.co\.cc$  [NC]
RewriteRule (.*) http://%1domain1.co.cc/$1 [R=301,L]

I found the www redirect here btw: Optimize htaccess Wildcard Subdomain Code

Now, what I want is all non-existent subdomains to get removed and the ones that exist (like "blog.domain1.co.cc" to stay.

I hope someone can help me with this. Thanks!

2
Can you provide more details e.g a few examples of what you would like to happen, define which domains exist etc.Ulrich Palha
I currently have 2 domains: discussions.domain1.co.cc and www.domain1.co.cc Any wildcard subdomains (like asdf.domain1.co.cc) should redirect to www.domain1.co.cc, but existing subdomains (like discussions.domain1.co.cc) should be excluded from this rule. examples: asdf.domain1.co.cc/article-2 --> www.domain1.co.cc/article-2 discussions.domain1.co.cc/thread-7 --> discussions.domain1.co.cc/thread-7 reitermarkus

2 Answers

1
votes
RewriteEngine On

#no longer needed
#RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
#RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

#don't redirect blog.example.com, forum.example.com and example.com
RewriteCond %{HTTP_HOST} ^((blog|forum)\.)?example\.com$
RewriteRule .* - [L]

#redirect the rest (including www.) to example.com
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
0
votes

Try adding the following to your htaccess file.

#if these lines already exist, skip them
RewriteEngine On
RewriteBase /

#if its not www or discussions subdomain
RewriteCond %{HTTP_HOST} !^(www|discussions)\.domain1\.co\.cc$ [NC]
#redirect to www domain
RewriteRule .* http://www.domain1.co.cc%{REQUEST_URI} [R=301,L]

For the following question

redirects subdomains and subdirectories to the other domain, just like that: forum.old.com/thread/12038213 --> forum.new.com/thread/12038213

Try

RewriteEngine On
RewriteBase /

#if domain is old.com
RewriteCond %{HTTP_HOST} ^(.+)\.old\.com$ [NC]
#redirect to new.com
RewriteRule .* http://%1.new.com%{REQUEST_URI} [L,R=301]