Blog

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.

Goals:

  1. Canonicalize the URLs. I chose www.jbspartners.com over http://jbspartners.com 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 www.jbspartners.com, rather than to www.jbspartners.com/index.php, which is the default for WordPress

Here is what the regular expressions look like.

# BEGIN WordPress

RewriteEngine On

RewriteCond %{HTTP_HOST} !^www.jbspartners.com
RewriteRule (.*) http://www.jbspartners.com/$1 [R=301,L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9} /index.php HTTP/
RewriteRule ^index.php$ http://www.jbspartners.com/ [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 www.jbspartners.com

The second RewriteCondition deals with resolving index.php to www.jbspartners.com

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 http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html

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

Comments are closed.