301 redirect with .htaccess

Discussion in 'Apache' started by jabz.biz, Jun 11, 2007.

  1. #1
    Hi,

    I have some problem, and could not find an answer online. I always used an .htaccess for redirecting, but it is not working properly with my new hoster. So, I have a problem.

    I have various domains, pointing in one hosting account all on the same website. Now I want to use only one of the domains to be the official domain, with www. Si I did this in my .htaccess:

    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^www.maindomain.com$ [NC]
    RewriteRule ^(.*) http://maindomain.com/$1 [L,R=301]


    So far, so good. It`s working. But now I want all the other domains also to redirect into this maindomain. Means: The user enters one of my domain names and gets redirected (301) to the www.- version of my maindomain.com

    Does anybody know what I have to do, to make it work?!
    Let me know, thanks! Your help is very much apprechiated.

    Jab
     
    jabz.biz, Jun 11, 2007 IP
  2. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #2
    Do the same thing at each domain.com/.htaccess
     
    Nintendo, Jun 11, 2007 IP
  3. drnibbles

    drnibbles Peon

    Messages:
    346
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Yes try Nintendos idea
     
    drnibbles, Jun 11, 2007 IP
  4. jabz.biz

    jabz.biz Active Member

    Messages:
    384
    Likes Received:
    6
    Best Answers:
    1
    Trophy Points:
    70
    #4
    Hmm, I found a good idea:

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

    The only question for me now is: Hoch do I get this redirect point into www.maindomain.com and not maindomain.com. Any ideas?

    Thanks.
     
    jabz.biz, Jun 11, 2007 IP
  5. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #5
    RewriteEngine on
    RewriteCond %{HTTP_HOST} .
    RewriteCond %{HTTP_HOST} !^maindomain\.com
    RewriteRule (.*) http://www.maindomain.com/$1 [R=301,L]
     
    Nintendo, Jun 12, 2007 IP
  6. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #6
    The RewriteCond also has to check against the www domain:

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

    .. otherwise it'll create an infinite loop.
     
    rodney88, Jun 12, 2007 IP
  7. jabz.biz

    jabz.biz Active Member

    Messages:
    384
    Likes Received:
    6
    Best Answers:
    1
    Trophy Points:
    70
    #7
    Thanks a billion for your help.

    BTW: Do you know a good online resource to get some more, or all information on htaccess coding?! I wanna dive into this...I always tried to avoid it, but I wanna learnit now. :)

    Thanks again for your help.
    Jab
     
    jabz.biz, Jun 13, 2007 IP
  8. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #8
    Look at the sticky thread!!!
     
    Nintendo, Jun 13, 2007 IP