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.
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?
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.
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):
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>
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.
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.