WordPress .htaccess Setup – Rewriting non www to www. for WordPress Blog Posts

This .htaccess file will rewrite non www to www.for all your post as well as canonicalize the URL’s for your posts as WordPress creates 4 seperate URL’s to each post and this .htaccess entry will help with this issue.

To help with SEO services I have also set my Permalink Settings to Custom Structure: /%postname%/ this means that whatever I choose to use as a title for my post becomes the URL of the post which in turn enhances the find-ability of any post on this blog in the search engines and gives a quick reference guide to any visitor that is visiting what my post is about. (On a side note; I feel WordPress should set this to be the default option for Permalinks or at least make the /%postname%/ a permanent radio button that users can choose from as it’s pretty much the first thing that is recommended when anyone is installing a new WordPress installation.)

Copy the following lines into notepad or another plain text editor and rename the file to .htaccess when it is uploaded to the root directory of your web server. Replace the domain name “yourdomain.co.nz” with your actual domain name. (This works for .com’s as well just change the domain suffix to ^yourdomain\.com or whatever is applicable for your particular domain suffix.)

# BEGIN WordPress
Options +Indexes
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^yourdomain\.co\.nz
RewriteRule ^(.*)$ http://www.yourdomain.co.nz/$1 [R=permanent,L]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://www.yourdomain.co.nz/$1/ [L,R=301]
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

Here is a picture of my Permalinks Section for reference sake. Setting this will help with your blogs search engine optimisation:

WordPress Permalink Setting

Posted in HowtoStuff | Leave a comment