Forum Moderators: phranque

Message Too Old, No Replies

Send all other tld's to .com version of address?

How to redirect .co.uk etc. to .com site?

         

mini2

4:34 pm on Oct 14, 2005 (gmt 0)

10+ Year Member



Hello,

I'm looking for the most complete/simple way to send visitors who try to enter my site via .co.uk or .net etc. (anything that's not .com) to the .com version of the URL they're requesting?

Is there a simple and efficient way to do this with htaccess, I'm sure there is but I'm hopeless at this side of things.

Thank you!

jdMorgan

4:39 pm on Oct 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



mini2,

Welcome to WebmasterWorld!

This question is essentially the same as the non-www to www domain redirects discussed in these threads [google.com]. The only difference is that you're basing the redirection on the tld being NOT(.com)

For more background information, see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].

Jim

mini2

4:51 pm on Oct 14, 2005 (gmt 0)

10+ Year Member



Thanks.

I've spent a lot of time looking, usually I manage to find the answer (hence the first post and join dates being so far apart!), but I couldn't spot what I was after this time.

I've taken another look and still don't really get "it".

I'm sure there's a way to get (for example) http://www.example.co.uk/forum/forumdisplay.php?f=1
to redirect to http://www.example.com/forum/forumdisplay.php?f=1
I just cant seem to get my head around it. Previously I've given up and used php to do it, but I'm sure htaccess will prove more efficient (if I can fathom it!).

[edited by: jdMorgan at 7:36 pm (utc) on Oct. 14, 2005]
[edit reason] No URLs, please. See TOS. [/edit]

mini2

5:07 pm on Oct 14, 2005 (gmt 0)

10+ Year Member



For example:

RewriteCond %{HTTP_HOST} !(www\.)example\.com
rewriterule (.*) http://www.example.com/$1 [R=301,L]

Works, as long as there's no path/file information passed, but if you add /forum it misses it.

[edited by: jdMorgan at 7:36 pm (utc) on Oct. 14, 2005]
[edit reason] No URLs, please. See TOS. [/edit]

jdMorgan

7:38 pm on Oct 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It should work just fine with the corrections shown below. The variable $1 in the substitution will be populated with the matched REQUEST_URI from the parenthesized pattern. This is called a "back-reference" in the Apache documentation.

RewriteEngine on
RewriteCond %{HTTP_HOST} !(www\.)?example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

If you put this code in .htaccess in your top-level Web-accessible directory, and it doesn't work, then there is something wrong with your server configuration that is preventing it from working. For example, if "/forum" is Aliased to another server path in httpd.conf, then this code won't be executed.

Jim