Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a server and a few domains and two webpages. Can't figure out how to do the following:

A.com -> root\
www.A.com -> root\
B.com -> root\
www.B.com -> root\
C.com -> root\folder1\
www.C.com -> root\folder1\

By the way. What is the 'and' logical operator used in htaccess? I found that 'or' is [OR] but [AND] doesn't seem to work. And what is the language htaccess is written in:)?

UPDATE

I made a mistake in the question though. Here's what I'd really want to do. DNS is set for the domain A.com to point to the root folder of the server. Now I would like to set the following redirections:

Any domain other than C.com and other than D.com redirects (301) to www.A.com. A.com points to the root folder of the server anyway and that is set in DNS.

Domain www.C.com points to the folder 'folder1' on the server. Can it be set in htaccess?

Now domains C.com, www.D.com and D.com redirects to www.C.com.

share|improve this question

migrated from stackoverflow.com Nov 29 '12 at 16:31

This question came from our site for professional and enthusiast programmers.

add comment

1 Answer 1

up vote 1 down vote accepted

For your A and B domains, use:

RewriteCond %{HTTP_HOST} ^(www\.)?(a|b).com$ [NC]
RewriteRule ^(.*)$ /root/ [L]

The (www\.)? allows you to use both with and without www (the ? means "the preceding block zero or one time").

(a|b) means "a or b". You can add more domains in there using more pipes (a|b|d|e)

For the other one:

RewriteCond %{HTTP_HOST} ^(www\.)?c.com$ [NC]
RewriteRule ^(.*)$ /root/folder1/ [L]

If you want www.A.com/hello.php to redirect to /root/hello.php then add $1 after the /root/ in the RewriteRule (goes for both rules)

[AND] does not exist because it is implicit when you use multiple RewriteConds. Something like this:

RewriteCond %{HTTP_HOST} ^www.a.com$ RewriteCond %{HTTP_HOST} ^www.b.com$

already means has [AND] implied and thus will never work (because a domain can never be a.com and b.com at the same time).


update Answer to your new question:

"Any domain other than C.com and other than D.com redirects (301) to www.A.com. A.com points to the root folder of the server anyway and that is set in DNS."

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

"Domain www.C.com points to the folder 'folder1' on the server. Can it be set in htaccess?"

RewriteCond %{HTTP_HOST} ^www.c.com$ [NC]
RewriteCond %{REQUEST_URI} !^/folder1/
RewriteRule ^(.*)$ /folder1/$1 [L,R]

"Now domains C.com, www.D.com and D.com redirects to www.C.com."

RewriteCond %{HTTP_HOST} ^c.com$ [OR]
RewriteCond %{HTTP_HOST} ^(www\.)?d.com$ [NC]
RewriteRule ^(.*)$ http://www.c.com/$1 [L,R=301]
share|improve this answer
    
I added a question to your question in a new question because it was too many characters. –  Marcin Kmiec Nov 29 '12 at 10:45
    
I updated my question. –  Marcin Kmiec Nov 29 '12 at 10:50
    
I updated my answer ;) –  Stephan Muller Nov 29 '12 at 10:51
    
The first part works. The second does not redirect to folder1. –  Marcin Kmiec Nov 29 '12 at 11:53
1  
Anyways thanks for a lot of help! I appreciate it very much. –  Marcin Kmiec Nov 29 '12 at 12:58
show 9 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.