1

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

2 Answers 2

1

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

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>
4
  • After change it not working.
    – ayyan
    Commented Mar 28, 2012 at 5:53
  • 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
  • @EugeneManuilov you need a / before $1 in the RewriteRule.
    – Geert
    Commented Mar 28, 2012 at 7:12
  • Thanks @Geert, it was early morning when I wrote it. I have updated it. Commented Mar 28, 2012 at 7:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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