redirect non www to www htaccess, Right code to use?

Discussion in 'Search Engine Optimization' started by nadlerz, Oct 12, 2011.

  1. #1
    1.
     rewritecond %{http_host} ^nadlerzforum.com [nc]    
    rewriterule ^(.*)$ http://www.nadlerzforum.com/$1 [r=301,nc]
    Code (markup):
    2.
     RewriteCond %{HTTP_HOST} !^www\.nadlerzforum\.com$    
    RewriteRule (.*) http://www.nadlerzforum.com/$1 [R=301,L]
    Code (markup):
    3.
    RewriteCond %{HTTP_HOST} !^www\.    
    RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
    Code (markup):
    4.
    RewriteCond %{HTTP_HOST} ^nadlerzforum.com [NC]    
    RewriteRule ^(.*)$ http://www.nadlerzforum.com/$1 [L,R=301]
    Code (markup):







    All of this 4 codes, works in my server.
    I'm not good in apache thing, my question is.,,.
    What is the best and right code to use?
    Thanks!
     
    nadlerz, Oct 12, 2011 IP
  2. karunverma

    karunverma Member

    Messages:
    92
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    45
    #2
    First one is good to use:

    rewritecond %{http_host} ^nadlerzforum.com [nc]
    rewriterule ^(.*)$ http://www.nadlerzforum.com/$1 [r=301,nc]

    However, others also works on your server but above format is most recommended by most of the webmasters. I also uses the same code on all of my websites :)
     
    karunverma, Oct 12, 2011 IP
  3. nadlerz

    nadlerz Well-Known Member

    Messages:
    264
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    188
    #3
    Thanks for the suggestion ;)
     
    nadlerz, Oct 12, 2011 IP
  4. Webmastergrace

    Webmastergrace Peon

    Messages:
    61
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Good explanation.Thanks for sharing.
     
    Webmastergrace, Oct 12, 2011 IP
  5. nadlerz

    nadlerz Well-Known Member

    Messages:
    264
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    188
    #5
    Lately I'm using this code:



    With \.com
    What is the difference with the first code and the second one?
     
    nadlerz, Oct 12, 2011 IP
  6. karunverma

    karunverma Member

    Messages:
    92
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    45
    #6
    Your Welcome :)
     
    karunverma, Oct 12, 2011 IP
  7. agendapal

    agendapal Peon

    Messages:
    80
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Yes all of them are working fine on my server as well.!!
     
    agendapal, Oct 13, 2011 IP
  8. danielwood

    danielwood Member

    Messages:
    115
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    28
    #8
    The first code says that if the requested URL start with nadlerzforum.com, redirect it to http://www.nadlerzforum.com and the second rule says that if URL nadlerzforum.com not starting with www. then redirect it to www version.
     
    danielwood, Oct 13, 2011 IP
  9. nadlerz

    nadlerz Well-Known Member

    Messages:
    264
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    188
    #9
    Then what is better to use?

    all small letters: rewritecond... ?
    Or with caps lock:
    RewriteCond... ?
     
    nadlerz, Oct 13, 2011 IP