htaccess redirect and links

Discussion in 'Site & Server Administration' started by Jboo, Jan 10, 2007.

  1. #1
    Hi,

    I've just added the following to my htaccess file to redirect www. visitors to the non-www. version of my site:

    RewriteCond %{HTTP_HOST} !^site\.com$ [NC]
    RewriteRule .* http://site.com/ [L,R=301]

    What I was wondering was if someone linked to my site with the www. would the link still count towards the non-www. version when using the redirect. The reason I ask is because people would naturally link to www. but I would still want this to have some effect rather than not counting as a backlink.

    Thanks for any help.
     
    Jboo, Jan 10, 2007 IP
  2. Richie_Ni

    Richie_Ni Illustrious Member

    Messages:
    10,721
    Likes Received:
    1,175
    Best Answers:
    0
    Trophy Points:
    410
    #2
    All the credits will go to the non-www domain,because it's a 301 redirect.
     
    Richie_Ni, Jan 10, 2007 IP
  3. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Yes, a 301 redirect is 'search engine friendly'. Also, regarding your rewriterules, you may want to redirect to the requested page rather than the homepage - if a user remembers the location of somewhere on your site and types in www.example.com/forums, they would prefer to be redirected to example.com/forums rather than just example.com

    RewriteCond %{HTTP_HOST} !^site\.com$ [NC]
    RewriteRule (.*) http://site.com/$1 [L,R=301]
    Code (markup):
    We simply capture the match in a backreference by putting it in parentheses. Then in the substitution, we call the backreference by $N (in this case N=1).
     
    rodney88, Jan 10, 2007 IP
    Jboo likes this.
  4. Jboo

    Jboo Active Member

    Messages:
    229
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    58
    #4
    Thank you Richie and Rodney, that's good to know.

    I was also going to ask the exact question you've answered for me Rodney (but it slipped my mind!), regarding inner page redirects, so thanks for adding that.
     
    Jboo, Jan 10, 2007 IP
  5. dcristo

    dcristo Illustrious Member

    Messages:
    19,776
    Likes Received:
    1,200
    Best Answers:
    7
    Trophy Points:
    470
    Articles:
    7
    #5
    That's good to know, but if you wanted to redirect to the www version of the requested page instead (other way around) how would you do this?
     
    dcristo, Jan 11, 2007 IP
  6. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #6
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule (.*) http://www.site.com/$1 [L,R=301]
    Code (markup):
    should do it.
     
    rodney88, Jan 11, 2007 IP
  7. dcristo

    dcristo Illustrious Member

    Messages:
    19,776
    Likes Received:
    1,200
    Best Answers:
    7
    Trophy Points:
    470
    Articles:
    7
    #7
    Thanks, but that seems to just redirect the requested non www version to the index page.
     
    dcristo, Jan 11, 2007 IP
  8. ArcticPro

    ArcticPro Banned

    Messages:
    1,273
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    0
    #8
    
    RewriteEngine On 
    RewriteCond %{HTTP_HOST} !^(.*)\.domain\.com$ [NC] 
    RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
    
    Code (markup):
    Try this. :D
     
    ArcticPro, Jan 14, 2007 IP
  9. dcristo

    dcristo Illustrious Member

    Messages:
    19,776
    Likes Received:
    1,200
    Best Answers:
    7
    Trophy Points:
    470
    Articles:
    7
    #9
    Thanks, seems to just redirect any requested non www page to the index page too.
     
    dcristo, Jan 14, 2007 IP
  10. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #10
    What pages are you trying to redirect (example url)? Do you have any other rewrite rules already in place?
     
    rodney88, Jan 14, 2007 IP
  11. dcristo

    dcristo Illustrious Member

    Messages:
    19,776
    Likes Received:
    1,200
    Best Answers:
    7
    Trophy Points:
    470
    Articles:
    7
    #11
    dcristo, Jan 14, 2007 IP
  12. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Is everything set up for rewriting? You could try the following two attempts but I wouldn't get your hopes up. The two rewrite rules already posted should work and I don't see why they're not.
    Options +Indexes
    Options +FollowSymlinks
    RewriteEngine on
    RewriteBase /
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule ^(.*)$ http://www.site.com/$1 [L,R=301]
    Code (markup):
    If still not, then another way of writing it would be:
    Options +Indexes
    Options +FollowSymlinks
    RewriteEngine on
    RewriteBase /
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule (.*) http://www.site.com/%{REQUEST_URI} [L,R=301]
    Code (markup):
     
    rodney88, Jan 14, 2007 IP