Welcome to WebmasterWorld Guest from

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Perm Redirect for 2 sites sharing IP address

.com diluting .co.uk PR effectiveness

10:27 am on Feb 25, 2004 (gmt 0)

10+ Year Member

Please advise if you have a situation like the following:
- 2 web sites sharing the same IP address and files.
- Each site getting independent links in - hence both sites have a PR of 5.

Rather than contacting the originators of the links, I'd like to have a permanent redirection put in to consolidate the .com links into the .co.uk site.
Have read much about 301 redirection - though I don't think this applies to apache server.

Any advice received if you have similar experience would be welcome.

3:26 pm on Feb 25, 2004 (gmt 0)

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


This is one of our most popular subjects here. Anyone whose site resolves at both www- and non-www domains is likely to have this problem - most aren't aware of it, though, just as most aren't aware of small details like pagerank. ;)

This code, when placed in the .htaccess file in your web root directory, will permanently redirect all domain-name variants except example.co.uk to example.co.uk. This includes uppercase and mixed-case variants, and also www vs. non-www variants. The result is that only the example.co.uk domain will remain in the search listings after a few months.

You can pick the domain variant you want to keep, and use it in both lines. But note that all literal periods should be escaped in the RewriteCond pattern (only) by preceding them with backslashes as shown.

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

In order to use this code, you must have "permission" to use mod_rewrite on your server. Some hosts allow it, and some don't.

Ref: Introduction to mod_rewrite [webmasterworld.com]


5:16 pm on Mar 13, 2004 (gmt 0)

10+ Year Member

I am one of the many having the same problem as Calum. Therefore I eagerly tested Jim's code. It does work, but I noticed three things:
(i) If I enter the full address "example.co.uk/index.html" in the browser the page is not to be found although it exists.
(ii) I cannot force the code to redirect only to "example.co.uk/", i.e. with the end slash. I learn that it is good to always use the /.
(iii) If exactly "EXAMPLE.CO.UK" is entered in the browser's address bar the code will not force the address to show up in lower case.
I admit being a newbie, so it is probably all my own fault.

Is it possible to combine this code with somethinmg else? Say I have three domains, www.aaaa.com, www.bbbb.com and www.cccc.com all pointing at the same IP. Now I know how to consolidate www.aaaa.com and www.bbbb.com. But if I would like a request for www.cccc.com to deliver the index page of the subdirectory www.cccc.com/sub/ can it also be done?

6:57 pm on Mar 13, 2004 (gmt 0)

10+ Year Member

Good questions Geekay - resolutions Jim?

Realised that I'm misinformed - I'm on a windows server and not an apache. Am also painfully new to this. So am wondering whether the file and code are the same?

Thx - Calum

8:02 pm on Mar 13, 2004 (gmt 0)

10+ Year Member

Domain names are case insensitive anyway, ie www.example.org, WWW.EXAMPLE.ORG and Www.ExaMple.org are all the same domain. It would be an error on Google's part to treat them as different domains.

As for the trailing / for www.example.org/, doesn't Internet Explorer always drop it from the location bar?


8:29 pm on Mar 13, 2004 (gmt 0)

10+ Year Member

If Google does *not* treat upper case and lower case domains as different domains, then everything is fine regarding my point (iii). It is the surfer's visual problem. I just got the impression the code would fix that too. My site's server log writes everything in lower case.
My Internet Explorer (ver. 6.0.28000) address bar does not drop the /. It is there if submitted, and omitted if not submitted.
4:34 am on Mar 15, 2004 (gmt 0)

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


The symptoms you report lead me to believe that some outside factor is interfering with the execution of your .htaccess file. This can be any number of things, from an improperly-configured host to badly-written server-level mod_rewrite code. Something is causing your server to skip processing your mod_rewrite code under certain circumstances.

The posted code is dirt-simple. You might want to send it to your host's tech support crew, and ask why it does not work under the circumstances you cited. Once you get it working, then expanding it to cover your various aaaa, bbbb, and cccc domains should be simple. You may want to ask them if they have set UseCanonicalName to on, as I recall this causing some problems.


10:16 am on Mar 18, 2004 (gmt 0)

10+ Year Member


Same question again on the O/S. Does the code you have generously shared work on Windows platforms?

Thx, Calum

4:01 pm on Mar 18, 2004 (gmt 0)

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

Yes and no...

mod_rewrite is an Apache module, so it's not present on a Windows server. However, there is a module called ISAPI Rewrite that is available to emulate the function of mod_rewrite on a Windows server. Try a WebmasterWorld search on ISAPI Rewrite [google.com] for some related threads.


6:36 pm on Mar 21, 2004 (gmt 0)

10+ Year Member

Now the code

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST}!^example\.co\.uk
RewriteRule (.*) [example.co.uk...] [R=301,L]

that did not work perfectly for me one week ago seems to perform OK. I should leave it at that, because this coding really is too difficult for me. But unfortunately I cannot. Now requests for www.aaaa.com and wwww.bbbb.com both result in www.aaaa.com showing in the browser's location bar. That was my first goal.

I also needed to redirect requests for www.cccc.com to www.cccc.com/sub/. All three domains, a, b and c, point to the same IP but I would like them to look like two separate sites. Now a request for www.cccc.com/sub/ will show up as wwww.aaaa.com/sub/ in the browser, and that is bad. I searched 200+ threads without finding one matching my needs. Are they really that unusual? The closest match was (adapted from forum92/1157.htm):

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?cccc\.com
RewriteRule \.html$ [%1cccc.com...] [R=301,L]

but that is not the correct code, not even when standing alone. Have you still got some patience with me?

5:46 am on Mar 22, 2004 (gmt 0)

10+ Year Member

Another, perhaps simplier approach, could be a code doing the following:

"If w.a/index or w.a or a (etc)
or w.b/index or w.b or b
then get w.a/index,
if w.c/index or w.c or c
then get w.c/sub/index"

3:08 pm on Mar 22, 2004 (gmt 0)

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


Adding a "bypass" for "cccc" domain ahead of your existing code, and using the [L] flag should work:

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

Note that the first rule is an internal rewrite only; it will not update the browser address bar, and so it "hides" the fact that you are using a subdirectory. It just looks "cleaner," and this is the typical way to use subdirectories to host subdomains. But the logic should work even if you decide to actually do an external redirect.
<edit> Corrected missing space before "!" </edit>

[edited by: jdMorgan at 11:34 pm (utc) on Mar. 22, 2004]

8:38 pm on Mar 22, 2004 (gmt 0)

10+ Year Member

Unfortunately it does not work. :( The new code that would have been such an elegant solution causes an Internal Server Error. When I remove the last two lines I find that it is the beginning that causes the error. Not even the simple code below works, it just opens the cccc.com index page:

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^cccc\.com/$ cccc.com/sub/ [R,L]

It is like there is something preventing redirection from the root index page to the /sub/ index page (provided my above test code is correct). I also tested with http etc. in the addresses, as well as other simple alternatives, but nothing seems to move me automatically to /sub/. Is there anything in the code of message 12 that could be further adjusted?

11:35 pm on Mar 22, 2004 (gmt 0)

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

Yes, posting on this board deletes spaces ahead of the "!" character. See the correction above.


10:20 am on Mar 23, 2004 (gmt 0)

10+ Year Member

After many hours of research and 100+ tests I finally think I have a code that does exactly what I want. I do not know why, but by adding something called request_uri I get the first rule working. Unfortunately it served the correct page, but with the address as www.aaaa.com/sub/ (not wwww.cccc.com/sub/), obviously because somehow the first [L] is not obeyed at all. So I removed the negation from rule two and listed all the domains separately instead.

RewriteCond %{HTTP_HOST} ^(www\.)?cccc\.com [NC]
RewriteCond %{REQUEST_URI} !^/sub1.*¦/sub2.*$
RewriteRule (.*) /sub1/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?bbbb\.com¦^(www\.)?dddd\.com¦^aaaa\.com [NC]
RewriteRule (.*) [aaaa.com...] [R=301,L]

I do not fully know what I am doing, so please tell me if the above less elegant code does not mean:
"If request is not for (www.)cccc.com/sub1/ (and whatever may follow) or /sub2/, then serve www.cccc.com/sub1/" (This is to prevent a request for just www.cccc.com to result in www.aaaa.com being served.)
"If request is for something containing (www.)bbbb.com or (www.)dddd.com, then serve www.aaaa.com/" (I added the ^aaaa\.com at the end so that a request for aaaa.com will be corrected to www.aaaa.com/)

P.S. That space before ! was missing only from my post 10, not from the code I used. I hope it is there in this post.

6:36 pm on Mar 23, 2004 (gmt 0)

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

Here's how I'd do it:

RewriteRule ^sub1¦sub2/ - [L]
RewriteCond %{HTTP_HOST} ^(www\.)?cccc\.com [NC]
RewriteRule (.*) /sub1/$1 [L]
RewriteCond %{HTTP_HOST} ^((www\.)?(bbbb¦dddd)¦aaaa)\.com [NC]
RewriteRule (.*) http://www.aaaa.com/$1 [R=301,L]

This code will not show that cccc.com uses a subdirectory -- It will look just like a 'real' subdomain.


9:06 pm on Mar 23, 2004 (gmt 0)

10+ Year Member

Excellent! This very clean and nice code also eliminates a problem with my own amateurish effort: if, within the cccc domain, a non-existing page was requested (like www.cccc.com/sub1/noex.html) a 404 would not be returned. I presume there was an infinite loop.

The very first row I altered to ^sub1¦sub2, because the / after sub2 resulted in a 404 error if www.cccc.com/sub2 (without the /) was requested. I hope the slash had no special meaning, or should there be a $?

Thank you, Jim. You have been very kind and patient.


Featured Threads

Hot Threads This Week

Hot Threads This Month