Do I have a proper 301 redirect?

Discussion in 'Apache' started by AdamFL, May 11, 2010.

  1. #1
    Hello, I have a website redirecting from www to non-www. I'm using the following .httaccess:
    
    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^example\.com$
    RewriteRule (.*) http://example.com/$1 [R=301,L]
    Code (markup):
    I also learned that my host has the option build in the control panel and i also set it up to redirect to non-www version of my website.
    My question is my .httacess code correct? and do i have to do anything else to not interfere with the cpanel assuming apache setting? i'm afraid that something could be wrong and upseting search engines.

    Thank you Kindly
     
    AdamFL, May 11, 2010 IP
  2. vinoth.t

    vinoth.t Peon

    Messages:
    156
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I'm not sure its working. If you are using FF install "Live HTTP Header" You can easily track the header information

    Use like below
    
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^www.domain.com$
    RewriteRule ^(.*) http://domain.com/$1 [L,R=301] 
    
    
    Code (markup):
     
    vinoth.t, May 12, 2010 IP
  3. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #3
    That looks fine to me, AdamFL.
     
    joebert, May 14, 2010 IP
  4. AdamFL

    AdamFL Well-Known Member

    Messages:
    791
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    120
    #4
    Thank you. while looking online I found the followng example:

    RewriteEngine On
    
    RewriteCond %{HTTP_HOST} ^www\.exampleforum\.com$
    RewriteRule ^(.*)$ http://exampleforum.com/$1 [L,R=301]
    Code (markup):
    I was wondering which one is better the first or the second, the only difference is the "^www\."
    Thank you
     
    AdamFL, May 14, 2010 IP
  5. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #5
    Performance-wise the difference is negligible.

    Technically the one that doesn't explicitly include www could match other subdomains, but since sub-domains are typically served from sub-directories that probably wouldn't matter for you, unless you have sub-domains that are served from the same DocumentRoot and just go to different files or something. Basically, if you have multiple domains reading the same htaccess it could be a problem.

    Other than that, I say just use whichever one you can look at and immediately remember what it means. Some people have trouble remembering that an exclamation mark means NOT, others may forget what the rule is for without the WWW in there somewhere.
     
    joebert, May 14, 2010 IP
  6. AdamFL

    AdamFL Well-Known Member

    Messages:
    791
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    120
    #6
    Thank you Joe - I had somebody else suggesting the following code:

    RewriteCond %{HTTP_HOST} !^domain\.com
    RewriteRule (.*) http://domain.com/$1 [L,R=301]
    Code (markup):
    Now i'm confused, I just want to know which one to add :) Thank you
     
    AdamFL, May 15, 2010 IP
  7. ROOFIS

    ROOFIS Well-Known Member

    Messages:
    1,234
    Likes Received:
    30
    Best Answers:
    5
    Trophy Points:
    120
    #7
    here's one tried and tested - works fine for server and with the search engines,

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

    just replace example with your site name. :cool:




    ROOFIS
     
    ROOFIS, May 15, 2010 IP
  8. AdamFL

    AdamFL Well-Known Member

    Messages:
    791
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    120
    #8
    will do what's the: Options +FollowSymLinks for? I'm just curious.

    Thank you :)
     
    AdamFL, May 15, 2010 IP
  9. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #9
    joebert, May 15, 2010 IP