Submit Your Site For Free!

Email Address:
* URL:
*
*Indicates Mandatory Field

Terms & Conditions

DevWebProAU
FlashNewz
DevWebPro








The Ultimate Wordpress Htaccess File

By Andy Beard
Expert Author
Article Date: 2007-04-03

Canonicalization is a serious problem for webmasters, just read this latest entry by Matt Cutts or this great post from John Andrews.

However telling webmasters that they should fix these issues isn't enough, webmasters & bloggers need solutions.

In Search of the Ultimate Htaccess file

A couple of months ago Alister Cameron posted a simple solution to .htaccess such that you didn't need to use a plugin to convert URLs using www to URLs without.

At the time I suggested a couple of improvements, and also mentioned I would post about it here on my blog, hopefully to help develop what could be looked on as the "Ultimate" .htaccess file for Wordpress, something you could just drop in your root folder and be done with it.
For me the inclination was for multiple niche websites using Wordpress as a CMS, so I really wanted to avoid anything that would make the content look dated.
I am not an htaccess guru, and this is all cobbled together from code suggested by other people in various places.Before using any of this code, make a backup of your existing .htaccess, and be prepared to copy it back if testing proves something is broken.
Lets start off with the default .htaccess for Wordpress once you turn on mod_rewrite for SEO friendly URLs:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
The first thing we want to do is get rid of the WWW if someone uses it. I know there are 2 schools of thought on whether URLs should have www by default or not, I prefer without and never type www unless I can't access another site without it (broken htaccess).

Secondly we also want to get rid of trailing slashes.

The base rules that Alister first suggested were:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.alistercameron.com$ [NC]
RewriteRule ^(.*)$ http://www.alistercameron.com/$1 [R=301,L]

RewriteCond %{REQUEST_URI} ^/[^.]+[^/]$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [R=301,L]
</IfModule>
However we want this to be the Ultimate htaccess code, thus we don't want to have to enter the domain name. I am not sure whether this will work if you have multiple blogs in sub-folders.

In this code we are using HTTP_HOST rather than adding a URL manually to every .htaccess file you create. If you are setting up 50 blogs (niche marketers do things like this, and fill them with unique original content - not everyone creates splogs) then being able to use one default file is a major advantage.
# If subdomain www exists, remove it first
RewriteCond %{HTTP_HOST} ^www.([^.]+.[^.]+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
Can we improve on the trailing slashes code?

Possibly…

A while ago I was also reading a post over on Aaron Walls SEO Book blog. Within the comments were suggestions with improvements to the code Aaron suggested.

Finding the exact reference is a problem as it wasn't on this thread.

Searching on a phrase in the code these days only brings up a reference on Alister's blog where I mentioned it in the comments, so I have no idea who to attribute this htaccess code to.
# If requested resource does not exist as a file
RewriteCond %{REQUEST_FILENAME} !-f
# and does not end with a period followed by a filetype
RewriteCond %{REQUEST_URI} !..+$
# and does not end with a slash
RewriteCond %{REQUEST_URI} !/$
# then add a trailing slash and redirect
RewriteRule (.*) $1/ [R=301,L]
</IfModule>
I am not a htaccess guru, but this seems to take into account more potential situations such as files to download.
If you put all this code together you end up with something like this

<IfModule mod_rewrite.c>
RewriteEngine On
# If subdomain www exists, remove it first
RewriteCond %{HTTP_HOST} ^www.([^.]+.[^.]+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

# If requested resource does not exist as a file
RewriteCond %{REQUEST_FILENAME} !-f
# and does not end with a period followed by a filetype
RewriteCond %{REQUEST_URI} !..+$
# and does not end with a slash
RewriteCond %{REQUEST_URI} !/$
# then add a trailing slash and redirect
RewriteRule (.*) $1/ [R=301,L]
</IfModule>

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress
So can anyone offer any constructive improvements?

If you offer improvements, please provide code samples and explain exactly why it is an improvement so people can learn from it (as I said I am a newbie at this)

Code can be entered using code tags in square brackets.

Comments

*Originallyl published at AndyBeard.eu

About the Author:
Andy Beard - Niche Marketing - Blog search engine perfomance, Wordpress and general niche and affiliate marketing tips




Newsletter Archive | Article Archive | Submit Article | Advertising Information | About Us | Contact

Dev Web Pro AU is an iEntry, Inc.® publication - 1998-2008 All Rights Reserved Privacy Policy and Legal