7

I have a WordPress installation that is not redirecting URLs not starting with www.

Example: http://example.com/dir/ doesn't send to http://www.example.com/dir/ instead goes to http://example.com/.

How do I change the .htaccess below to always redirect to the page either using www or not?

# BEGIN WordPress
RewriteEngine On

RewriteBase /
RewriteRule ^index\.php$ - [L]

# uploaded files
RewriteRule ^([_0-9a-zA-Z-]+/)?files/(.+) wp-includes/ms-files.php?file=$2 [L]

# add a trailing slash to /wp-admin
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule  ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) $2 [L]
RewriteRule  ^([_0-9a-zA-Z-]+/)?(.*\.php)$ $2 [L]
RewriteRule . index.php [L]

# END WordPress
8

This snippet will force the www to always be in thew URL. I don't know if that helps you or not:

Options +FollowSymlinks
RewriteCond %{HTTP_HOST} !^(www\.|$) [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
  • 3
    +1 ... and this snippet should work in all cases if you put it before the RewriteRule ^index\.php$ - [L] directive. – danlefree Nov 12 '10 at 2:35
  • danlefree, thanks for that note. That small tip fixed my issue. – Jeff K. May 8 '12 at 0:25
0

Altering the .htaccess shouldn't be necessary. You should only need to change the website URL under Settings -> General in the WP Admin to include the www.

If you try John's answer and you have the website URL in WP settings set to the non-www version, you will create a redirect loop.

0

I would mix the two answers above

  1. you HAVE to set properly the website URL So that the CMS always prints the correct address
  2. you can set a proper rewrite rule so that in case someone links the wrong URL (no-www) you don't end up with Google indexing all pages twice penalizing the SEO ranking

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