Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

In wordpress site how to Redirect the non-www version of the site to the www version to remove duplicate content

share|improve this question
add comment

2 Answers

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>
share|improve this answer
    
After change it not working. –  ayyan Mar 28 '12 at 5:53
    
I have updated the code. I forgot to add ,L directive to the redirection rule. It should work now. –  Eugene Manuilov Mar 28 '12 at 6:05
    
@EugeneManuilov you need a / before $1 in the RewriteRule. –  Geert Mar 28 '12 at 7:12
    
Thanks @Geert, it was early morning when I wrote it. I have updated it. –  Eugene Manuilov Mar 28 '12 at 7:46
add comment

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>
share|improve this answer
    
hello geert, till my site not redirect. any other solution for redirecting www and non-www. –  ayyan Mar 30 '12 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 –  ayyan Mar 30 '12 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. –  Geert Mar 30 '12 at 7:25
    
hellow geert i put the code in .htaccess. But it redirect out of the site. –  ayyan Mar 30 '12 at 7:29
    
You did change "yourdomain.com" to your own domain, did you? –  Geert Mar 30 '12 at 8:31
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.