How to modify .htaccess to resolve index.php and non-www to www in WordPress

Editing the .htaccess file, for those of you that don’t host on a Windows server, can be tricky.


  1. Canonicalize the URLs. I chose over because it is what people expect, it is easier to type and I like it. Others may choose to drop the www before the domain name.
  2. Apply the above consistently throughout the site.
  3. Force the Home page to resolve to, rather than to, which is the default for WordPress

Here is what the regular expressions look like.

# BEGIN WordPress

RewriteEngine On

RewriteCond %{HTTP_HOST} !^
RewriteRule (.*)$1 [R=301,L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9} /index.php HTTP/
RewriteRule ^index.php$ [R=301,L]

RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

# END WordPress

How it works:

The first RewriteCondition deals with resolving the URL to

The second RewriteCondition deals with resolving index.php to

The last two RewriteCond come with WordPress

The # (number sign) is used for comments, which the parser ignores.

The % is the variable symbol

The ! means Not for pattern matching. As in when the URL is not www.

The ^ means start of string.

The L stands for Last and tells Apache to terminate the rewrite rule and rewrite condition. Notice the L is used at the end of each paragraph.

The R stands for Redirect. 302 is temporary and 301 is permanent. This redirections is visible in the URL window of the browser.

-f matches an existing file name

-d matches an existing directory name

To Read more visit

One thought on “How to modify .htaccess to resolve index.php and non-www to www in WordPress”

Comments are closed.