views:

238

answers:

4

I have a domain domain.com that I use as my primary domain. I also own a second domain domain2.com that I have automatically go to domain.com

This is done with .htaccess:

RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
RewriteRule (.*) http://www.domain.com/$1 [L,R=301]

I want to set up a subdomain sub.domain.com but with that current .htaccess, visiting sub.domain.com sends me to www.domain.com/sub

I found this question, which solves this problem, but negates my first issue of domain2.com pointing to domain.com

RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

I'm looking for a way to combine the two rules:

www.domain.com => www.domain.com
domain.com => www.domain.com
domain2.com => www.domain.com
sub.domain.com => sub.domain.com

+1  A: 

You'd need to tighten up your RewriteConds so that they only matched the specific hostnames you wanted to match and change, like ‘^domain\.com$’ and ‘^(www\.)?domain2\.com$’. At the moment they will both match pretty much anything, so they'll interfere.

But do you have to do this in .htaccess? It's much cleaner to do with a simple Redirect in your main site config:

<VirtualHost *:80>
    ServerName www.domain.com
    ...your real config...
</VirtualHost>

<VirtualHost *:80>
    ServerName www.domain2.com
    ServerAlias domain2.com
    ServerAlias domain.com
    ServerAlias sub.domain.com
    Redirect permanent http://www.domain.com/
</VirtualHost>

which also gets the nasty deployment-specific stuff out of your application's web root.

(I couldn't work out whether you actually wanted that the subdomain went to a different folder; if you do, remove the sub.domain.com line above and put it into its own VirtualHost:)

<VirtualHost *:80>
    ServerName sub.domain.com
    Redirect permanent http://domain.com/sub/
</VirtualHost>
bobince
I'm not familiar with virutal host stuff or where to do any of this. I certainly don't HAVE to do this in .htaccess, but it's the only place I know how to (and I'm not even very good with that!)
idrumgood
I think he means, do you have access to httpd.conf?
Kev
+1  A: 

Try this rule:

RewriteCond %{HTTP_HOST} !^([^.]+\.)?example\.com$ [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.example.com%{REQUEST_URI} [L,R=301]
Gumbo
This is very close to working. Out of the 4 cases listed at the bottom of the question, all work but the second one. If I just go to `domain.com` it doesn't add the www (which I would like it to do).
idrumgood
+1  A: 

Would this simply work :

RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^sub\.domain\.com$ [NC]
RewriteRule (.*) http://www.domain.com/$1 [L,R=301]
MaxiWheat
+1  A: 
RewriteCond %{HTTP_HOST} !^([^.]+\.)example\.com$ [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.example.com%{REQUEST_URI} [L,R=301]

Should solve your problem :D

Mez
This is the one that works, however, I can't seem to accept the answer. Sorry Mez.
idrumgood
Weird... Wonder who the bounty went to if you cant accept an answer
Mez