Forum Moderators: phranque

Message Too Old, No Replies

Easier Mod_Rewrite Code

domain.com www.domain.com multiple domains

         

Moff

6:31 pm on Jul 14, 2004 (gmt 0)

10+ Year Member



Hi,

I currently have two domains, all hosted on the same account. Basically what I want to do is make sure that no matter how either domain is typed ie with or without the www.prefix then [domain1.co.uk...] is shown.

I've managed to do it but not without using 3 seperate RewriteCond rules. I think what I think im asking is: is there a way to have an OR statement in a rewriteCond rule?

Here's what I currently have (seems a little long winded?)


Options +FollowSymLinks
rewriteEngine on
RewriteCond %{HTTP_HOST} ^domain1\.co\.uk [NC]
RewriteRule ^(.*)$ http://www.domain1.co.uk/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^domain2\.co\.uk [NC]
RewriteRule ^(.*)$ http://www.domain1.co.uk/$1 [R=301,L]
RewriteCond %{HTTP_HOST}!^www\.domain1\.co\.uk
RewriteRule (.*) http://www.domain2.co.uk/$1 [R=301,L]

gergoe

6:54 pm on Jul 14, 2004 (gmt 0)

10+ Year Member



Most definitely; check out the OR flag in the documentation of RewriteCond [httpd.apache.org].

Moff

7:59 pm on Jul 14, 2004 (gmt 0)

10+ Year Member



Brilliant, thanks for the link. Am I right in assuming the No Case Flag is only required for the last Condition?

Moff

jdMorgan

8:18 pm on Jul 14, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



[NC] makes the pattern match case-insensitive. I would assume you would want to use it in all cases shown.

You've got two "redirect if" rules above, and the last one is a "redirect if not" rule - the difference is the exclamaton point ("!" is the logical NOT operator) in front of the last rule's RewriteCond pattern. As shown, it will lead to an infinite loop, because a request to www.domain2.co.uk is not going to match ^www\.domain1\.co\.uk, so that rule will be invoked, redirecting www.domain2.co.uk to www.domain2.co.uk. This redirect will repeat until the browser or server reaches its redirection limit, at which point, you'll get an error message.

Whatever you do, don't install your code unless you understand exactly what it is going to do... :o

Jim

Moff

8:19 pm on Jul 14, 2004 (gmt 0)

10+ Year Member



Sorry, take the last question back. Think i found the answer. Cant seem to get the last condition to work though.

New Code is:


Options +FollowSymLinks
rewriteEngine on
RewriteCond %{HTTP_HOST} ^domain1\.co\.uk [NC,OR]
RewriteCond %{HTTP_HOST} ^domain2\.co\.uk [NC,OR]
RewriteCond %{HTTP_HOST}!^www\.domain2\.co\.uk [NC]
RewriteRule (.*) http://www.domain1.co.uk/$1 [R=301,L]

Can anyone give me any pointers?

Much appreciated!

Moff

8:21 pm on Jul 14, 2004 (gmt 0)

10+ Year Member



Ah, thanks JdMorgan

guess that answers the last question too.

jdMorgan

8:23 pm on Jul 14, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here's a twist to make two rules do the work of three:

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

Jim

jdMorgan

8:30 pm on Jul 14, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



... and finally, the shortest method to redirect any domain except www.domain1.co.uk:

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

(I suspect you found this method, but mixed it in with the others)

Jim

Moff

8:48 pm on Jul 14, 2004 (gmt 0)

10+ Year Member



Wow, thanks Jim.

Your quite right in that that was the method I found which was why I'd forgotten to take out the exclamation mark on the final rule.

First time I'd tried it though my browser was keeping the original URL, which made me think I'd done it wrongly - guess I really should have looked into it a bit more thoroughly before jumping in.

Anyway, thanks for all your help!

Moff