Forum Moderators: phranque
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.
Calum
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]
Ref: Introduction to mod_rewrite [webmasterworld.com]
Jim
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?
As for the trailing / for www.example.org/, doesn't Internet Explorer always drop it from the location bar?
Jon.
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.
Jim
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.
Jim
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?
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]
[edited by: jdMorgan at 11:34 pm (utc) on Mar. 22, 2004]
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?
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.
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]
Jim
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.