Redirecting www to non-www?

Discussion in 'Search Engine Optimization' started by Domoner, Feb 13, 2014.

  1. #1
    Hi, I'm using wordpress and I want to use example.com instead of www.example.com

    I read the best method to avoid problems in the search engines is to go into cpanel and edit the .htaccess file to redirect. However I found 2 difference pieces of code online and since I'm not up on this, just needed some help.



    FIRST SAYS THIS CODE:

    <ifmodule mod_rewrite.c="">
    RewriteEngine On
    RewriteBase /
    RewriteCond % ^www.example.com [NC]
    RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
    </ifmodule>




    SECOND SAYS THIS CODE:

    RewriteEngine on
    RewriteBase /

    #Force non-www:
    #RewriteCond %{HTTP_HOST} www.example.com(.*)$ [NC]
    #RewriteRule ^(.*)$ http://example.com%1/$1 [R=301,L]



    Really appreciate some help. Thanks.
     
    Domoner, Feb 13, 2014 IP
  2. patco

    patco Well-Known Member

    Messages:
    2,035
    Likes Received:
    47
    Best Answers:
    17
    Trophy Points:
    100
    #2
    Where do you want to post those codes? Don't you have an access to the HTML code where everything is much easier... And also I have another question... Why do you need to do such a redirection?
     
    patco, Feb 13, 2014 IP
  3. competent123

    competent123 Notable Member

    Messages:
    1,750
    Likes Received:
    71
    Best Answers:
    6
    Trophy Points:
    255
    #3
    you dont' have to do that stuff
    just goto wordpress settings>general, and put your url as www.example.com
    wordpress will do it by itself, you dont' need to bother with these codes.
     
    competent123, Feb 13, 2014 IP
  4. RobinInTexas

    RobinInTexas Active Member

    Messages:
    217
    Likes Received:
    14
    Best Answers:
    2
    Trophy Points:
    65
    #4
    You don't NEED to do it unless you care about site load time for those who use the incorrect form and try your site with the www in front. While it is true that WordPress will do the redirect, it adds about a half second to the site load time for WordPress to issue the correction.

    This code will work:
    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
    RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
    Code (markup):
     
    RobinInTexas, Feb 13, 2014 IP
  5. wordpecker

    wordpecker Well-Known Member

    Messages:
    743
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    110
    #5
    Both the codes are same. The syntax of mod rewrite is impossible to understand (to me at least :) ) but the code works and I am using the same in one of my WP sites. <ifmodule mod_rewrite.c=""></ifmodule> are start and closing tags.

    Open the .htaccess in notepad or npp and paste the code just above the existing code. Save the file and upload it via ftp (if using file explorer just save it).

    Since you are showing the non-www version WordPress Address (URL) is already set to http://xyz.com. No need to use the following mod rewrite rule.
    <ifmodule mod_rewrite.c="">
    RewriteEngine On
    RewriteBase /
    RewriteCond % ^www.example.com [NC]
    RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
    </ifmodule>
     
    wordpecker, Feb 13, 2014 IP
  6. Domoner

    Domoner Greenhorn

    Messages:
    44
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    8
    #6
    Thanks all most helpful,

    But sorry "wordpecker" were you saying not to use that code which you pasted there and to go ahead with the other one? Robin said the other one too.

    Thanks.
     
    Domoner, Feb 14, 2014 IP
  7. RobinInTexas

    RobinInTexas Active Member

    Messages:
    217
    Likes Received:
    14
    Best Answers:
    2
    Trophy Points:
    65
    #7
    I haven't looked very closely at the other codes posted, after a quick look I would say any of them will work. I know the code I posted works.

    Almost any typo anywhere in an htaccess file will result in a 500 error, just test any changes you make immediately after uploading, and you should be ok-- undo any changes if you get an error.

    I stand by my statement, that it's faster to use htaccess than to let WordPress do the work, I've tested it on several different hosting accounts.
     
    RobinInTexas, Feb 15, 2014 IP