Welcome to WebmasterWorld Guest from 3.227.2.109

Forum Moderators: Ocean10000 & phranque

Message Too Old, No Replies

Easier Mod_Rewrite Code

domain.com www.domain.com multiple domains

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

Junior Member

10+ Year Member

joined:Jan 21, 2004
posts:48
votes: 0


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]
6:54 pm on July 14, 2004 (gmt 0)

Preferred Member

10+ Year Member

joined:May 4, 2004
posts:525
votes: 0


Most definitely; check out the OR flag in the documentation of RewriteCond [httpd.apache.org].
7:59 pm on July 14, 2004 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 21, 2004
posts:48
votes: 0


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

Moff

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

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


[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

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

Junior Member

10+ Year Member

joined:Jan 21, 2004
posts:48
votes: 0


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!

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

Junior Member

10+ Year Member

joined:Jan 21, 2004
posts:48
votes: 0


Ah, thanks JdMorgan

guess that answers the last question too.

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

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


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
8:30 pm on July 14, 2004 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


... 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

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

Junior Member

10+ Year Member

joined:Jan 21, 2004
posts:48
votes: 0


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