4

\I'm darn nearly pulling my hair out trying to figure this out tonight. I'm hoping someone can help me out.

I have 3 TLD's for a site, similar to the following:

  • www.domain.com
  • www.domain.org
  • www.domain.net

They are all located in the same directory.

I would like to set up 301 redirects so that all pages of the .org and .net point to their respective pages at the .com location.

For example, domain.net/topic/page as well as www.domain.net/topic/page should permanently redirect to www.domain.com/topic/page.

Currently, I am using the code below which only redirects the .net and .org home pages to the .com home page.

RewriteCond %{HTTP_HOST} !^(www\.)?domain\.com$ [NC]
RewriteRule .?$ http://www.domain.com%{REQUEST_URI} [R=301,L]

Thank you for your time,

Casey

C Dog
  • 157
  • 1
  • 3
  • 12

3 Answers3

4

This one is based on your example with minor modification:

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

NC, R=301 and L are pretty obvious. The NE is for no-escape and it prevents query string variables from being escaped twice. The ^(.*) does not need a / in most cases.

Note: 301 permanent redirect responses will be cached by the browser so clear your browser cache every now and then while testing. Otherwise you may not see the result of changes you make.

Salman A
  • 262,204
  • 82
  • 430
  • 521
  • Like mine, yours is only redirecting the home pages. For example, when I type in domain.net/about I still land on the .net version. – C Dog Oct 27 '11 at 13:53
  • It appears to be working on my system. Try opening a URL that does not exist on your website e.g. `domain.net/foobar?a=b` and see if you get sent to `domain.com/foobar?a=b` – Salman A Oct 27 '11 at 14:16
2
RewriteCond %{HTTP_HOST}   domain\.(org|net) [NC]
RewriteRule ^/(.*)         http://www.domain.com/$1 [L,R=301]

There are many other way to write the Cond, this is how I did mine - a positive compare. Yours is a negative compare, but it has the drawback that if someone doesn't send a host header it will redirect forever, on the plus you don't need to list all the domains to redirect. Also you can add a rule to redirect .com without www if you want.

For the Rule the trick is the () in the expression - this captures that part of the url for use later with the $1

Ariel
  • 25,995
  • 5
  • 59
  • 69
  • Thanks Ariel, unfortunately, nothing seems to happen when I plug in your Condition. – C Dog Oct 27 '11 at 02:23
  • Then it's time to simplify. Reduce the Cond to nothing - i.e. it matches everything, and make the rule static (no `$1`, just redirect to some simple page. Then slowly add things back in till it breaks. – Ariel Oct 27 '11 at 03:34
0
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com$1 [R=301,L]

This redirects every request to www.domain.com including querystring and request-uri if the host is not www.domain.com

Seybsen
  • 14,989
  • 4
  • 40
  • 73
  • Thank you for your help Seybsen - but this still seems to only redirect the home pages. For example, when I type in domain.net/about I still land on the .net version – C Dog Oct 27 '11 at 13:54
  • Can you please update your question and add your complete .htaccess or virtualhost config? The rewrite I posted is 100% working (I'm using it for the same case as you do) – Seybsen Oct 27 '11 at 15:02