Non-WWW to WWW

Discussion in 'Site & Server Administration' started by NickR25, Mar 12, 2009.

  1. #1
    I got the rewrite for visitors who go to mysite.com to be redirected to www.mysite.com and it works fine.

    The problem is that I cannot get this to work for a specific directory.

    I want users who navigate to mysite.com/directory to be redirected to www.mysite.com/directory.

    This is what I have in that specific directory's .htaccess (not my root's .htaccess):

    
    # Non-www to WWW
     RewriteCond %{HTTP_HOST} !^www\.mysite\.com\.directory[NC]
     RewriteRule (.*) http://www.mysite.com/directory/$1 [R=301,L]
    Code (markup):
    And for some reason, this results in a browser error telling me it is resulting in an infinite loop that will never end.

    Why is that? How do I fix it?

    Thanks :)
     
    NickR25, Mar 12, 2009 IP
  2. Camay123

    Camay123 Well-Known Member

    Messages:
    3,423
    Likes Received:
    86
    Best Answers:
    0
    Trophy Points:
    160
    #2
    this should work for domain and subfolder.
     
    Camay123, Mar 12, 2009 IP
  3. NickR25

    NickR25 Peon

    Messages:
    394
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Unfortunately, this doesn't work.

    I'd prefer if I could control just one specific directory of the domain, because my root is a WordPress blog, so it automatically redirects to the WWW version using the permalink settings.

    Is there a code I can place in the .htaccess of /directory (not the root) to redirect it to the WWW version?

    Thanks for your help ;)
     
    NickR25, Mar 13, 2009 IP
  4. internetstromer

    internetstromer Member

    Messages:
    19
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #4
    Hey NickR25,

    you can place the following code into your .htaccess file

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

    this will definatly work :) as its working on my server ;)
     
    internetstromer, Mar 17, 2009 IP
  5. JPC-NickO

    JPC-NickO Guest

    Messages:
    69
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Hi,

    You can also do something a bit more generic like

    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^www\..* [NC]
    RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]
     
    JPC-NickO, Mar 17, 2009 IP
  6. Dimma

    Dimma Well-Known Member

    Messages:
    631
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    140
    #6
    is there an error in this line
    RewriteCond %{HTTP_HOST} !^www\.mysite\.com\.directory[NC]
    must be a slash?
    RewriteCond %{HTTP_HOST} !^www\.mysite\.com\/directory[NC]
     
    Dimma, Mar 18, 2009 IP
  7. NickR25

    NickR25 Peon

    Messages:
    394
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #7
    None of the ones that have been posted in this thread work.

    Remember, I am wanting to redirect a specific directory only; not any URL. In other words, I want mysite.com/directory to redirect to www.mysite.com/directory, but I do not want mysite.com to redirect to www.mysite.com.

    Edit: Actually, I think we have a winner :D
    This worked when I placed it in my /directory:
    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^www\..* [NC]
    RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/directory/$1 [R=301,L]
    Code (markup):
     
    NickR25, Mar 18, 2009 IP
  8. NickR25

    NickR25 Peon

    Messages:
    394
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Update: It was working at first, but I just realized it no longer works.

    It tries to take me to www.mysite.com/hsphere/local/home/*****/mysite.com/directory

    Why is that and how do I correct it?
     
    NickR25, Mar 19, 2009 IP
  9. JPC-NickO

    JPC-NickO Guest

    Messages:
    69
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #9
    How about:

    
    
    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^www\..* [NC]
    RewriteCond %{REQUEST_URI} ^/directory [NC]
    RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
    
    
    Code (markup):
     
    JPC-NickO, Mar 19, 2009 IP
  10. NickR25

    NickR25 Peon

    Messages:
    394
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Should I put this in the .htaccess of the root or the directory?
     
    NickR25, Mar 20, 2009 IP
  11. droid

    droid Peon

    Messages:
    55
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Few days ago Google has indexed both - www_mysite and mysite O_0
     
    droid, Mar 21, 2009 IP