1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Need Help - Htaccess - rewrite www to http

Discussion in 'Black Hat SEO' started by SuperBlackHat, Aug 17, 2010.

  1. SuperBlackHat

    SuperBlackHat Power Member

    Joined:
    Feb 2, 2009
    Messages:
    576
    Likes Received:
    116
    I want to make www.mydomain.com redirect to http://mydomain.com. Does anyone know how to do this properly? I tried:

    Options +FollowSymlinks
    RewriteEngine on
    RewriteCond %{HTTP_HOST} !^www.mydomain.com$
    RewriteRule ^(.*)$ http://mydomain.com [R=301,L]

    The browser told me:

    Too many redirects occurred trying to open "http://mydomain.com/". This might occur if you open a page that is redirected to open another page which then is redirected to open the original page.

    Does anyone know the right code to use?
     
    Last edited: Aug 17, 2010
  2. SuperBlackHat

    SuperBlackHat Power Member

    Joined:
    Feb 2, 2009
    Messages:
    576
    Likes Received:
    116
    After a bit of searching, I found the solution for anyone curious:

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

    Cheers
     
  3. Entrepreneur

    Entrepreneur Regular Member

    Joined:
    Oct 12, 2007
    Messages:
    438
    Likes Received:
    379
    This is mine and it works fine. The only difference I can see it the RewriteBase / and the exclamation mark before condition?

    RewriteEngine On
    Options +FollowSymLinks
    RewriteBase /
    RewriteCond %{HTTP_HOST} ^www.domain.com [NC]
    RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]

    EDIT: LOL, beat me to it.
     
  4. zestoi

    zestoi Newbie

    Joined:
    Mar 9, 2010
    Messages:
    41
    Likes Received:
    3
    this is the correct solution.

    your first example meant if the URI is not exactly http://www.mydomain.com then redirect... so will create a loop

    your second example means if the URI starts with mydomain.com then redirect... so i don't see how it will work for http://www.mydomain.com

    the example by Entrepreneur means if the URI starts with http://www.mydomain.com then redirect...

    so really not sure how your working example actually works... having said that i am knackered and need to sleep :p
     
  5. SuperBlackHat

    SuperBlackHat Power Member

    Joined:
    Feb 2, 2009
    Messages:
    576
    Likes Received:
    116
    so are you saying my code is wrong? I tested it out and it seemed to be working fine
     
  6. zestoi

    zestoi Newbie

    Joined:
    Mar 9, 2010
    Messages:
    41
    Likes Received:
    3
    i just tried this and it didnt work - did nothing at all in fact... (domain name removed obviously)....

    Code:
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^xxxxxxxx.com
    RewriteRule (.*) http://xxxxxxxx.com/$1 [R=301,L]
    
    that's basically your example isn't it?
     
  7. SuperBlackHat

    SuperBlackHat Power Member

    Joined:
    Feb 2, 2009
    Messages:
    576
    Likes Received:
    116
    i dont know, when i test my site in the browser, it seems to work with no problems that i can see at least. the www redirects to http without a hitch.
     
  8. SuperBlackHat

    SuperBlackHat Power Member

    Joined:
    Feb 2, 2009
    Messages:
    576
    Likes Received:
    116
    ok this is odd.... if i type in www.mydomain.com in the browser, it stays there at www, without redirecting. Im trying entrepreneurs code and this is the result im getting.
     
  9. zestoi

    zestoi Newbie

    Joined:
    Mar 9, 2010
    Messages:
    41
    Likes Received:
    3
    exactly. your code won't do anything nasty if you dont put in the www but also wont do anything useful if you do put in the www.
     
  10. SuperBlackHat

    SuperBlackHat Power Member

    Joined:
    Feb 2, 2009
    Messages:
    576
    Likes Received:
    116
    Options +FollowSymLinks
    RewriteEngine on
    RewriteCond %{HTTP_HOST} .
    RewriteCond %{HTTP_HOST} !^mydomain\.com
    RewriteRule (.*) http://mydomain.com/$1 [R=301,L]


    This is the one i found to finally work.