In wordpress site how to Redirect the non-www version of the site to the www version to remove duplicate content
2 Answers
This code is similar to Eugene’s, but it will redirect any domain that is not your main domain with “www” to the correct address. Handy if you have multiple top-level domains.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# These are the lines that do the trick
RewriteCond %{HTTP_HOST} !^www\.yourdomain\.com$
RewriteRule .* http://www.yourdomain.com/$0 [L,R=301]
</IfModule>
-
hello geert, till my site not redirect. any other solution for redirecting www and non-www.– ayyanCommented Mar 30, 2012 at 6:26
-
# BEGIN WordPress <IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{HTTP_HOST} ^http:www.//demo.yourdomain.com/realestate/$ RewriteRule (.*) demo.yourdomain.com/realestate/$1 [L,R=301] RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d </IfModule> # END WordPress– ayyanCommented Mar 30, 2012 at 6:55
-
Note that you need to put the my code above the WP code in the
.htaccess
. Also, make sure your Apache server has the mod_rewrite module installed.– GeertCommented Mar 30, 2012 at 7:25 -
hellow geert i put the code in .htaccess. But it redirect out of the site.– ayyanCommented Mar 30, 2012 at 7:29
-
You need to setup it in your .htaccess file, it should looks like this one:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^yourdomain\.com$
RewriteRule (.*) http://www.yourdomain.com/$1 [L,R=301]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
-
-
I have updated the code. I forgot to add
,L
directive to the redirection rule. It should work now. Commented Mar 28, 2012 at 6:05 -
-
Thanks @Geert, it was early morning when I wrote it. I have updated it. Commented Mar 28, 2012 at 7:46