The Ultimate Wordpress Htaccess File?

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 slash problems

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.

Related posts

This entry was posted in Google, SEO Blog, blogging tips, mininet, vre, wordpress and tagged , , , , , , , , , . Bookmark the permalink. Both comments and trackbacks are currently closed.

23 Comments

  1. Lord Matt (42 comments.)
    Posted April 4, 2007 at 12:18 pm | Permalink

    In my expirence the use of [L,QSA] is often a life saver. It might not mean much but the number of times a cruftless URL got a ?foo=bar by a plugin (such as a multipage post) that doesn’t work…

    The solution is QSA Query String Append. If something is going to insist on passing by QS then you need to hand it over.

    Furthermore QS handling is native to php via $_GET[] and so passing the “fake” path data to the script means that PHP dos not need to work so hard.

    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?virtualpath=$1 [L,QSA]

    I have no idea if your platform can cope with this but one assumes it can. This worked with NucleusCMS – YMMV.

  2. Philip de Lisle (4 comments.)
    Posted April 25, 2007 at 9:41 pm | Permalink

    Andy

    Is there a reason why there are 2 blocks of “

  3. Philip de Lisle (4 comments.)
    Posted April 25, 2007 at 9:51 pm | Permalink

    Andy

    Is there a reason why there are 2 blocks of code in this if you are using WordPress (as I am)?

    Also I want to have the default to include the “www.” subdomain as it is the norm for people to think of (how many business cards have you seen without it as the companyt web address?). It might be a good idea to include this in the code in comments for people like me who are not comfortable with messing with a .htaccess file (hint!)

    [BTW this is a 2nd posting as my original appeared to be truncated when I used a double quote mark. Is this a bug in the blog software?]

  4. Andy Beard (1685 comments.)
    Posted April 25, 2007 at 10:00 pm | Permalink

    I am not sure what you are referring to. There are 5 blocks of code on the page that seem to display OK in both FF2.0+ and IE7

    The final block of code is the one I use all the time for blog now, although I did just hit a problem with my host deleting it for some reason, maybe by accident.

    It took me 30 seconds to fix the probelm when diagnosed, because I only have one .htaccess file to worry about, for all of my sites.

  5. Philip de Lisle (4 comments.)
    Posted April 25, 2007 at 10:07 pm | Permalink

    I’m having problems posting comments for some reason when quoting the code so I’ll use the line numbers ;)

    Block 1, as I see it, is lines 1 to 15 and block 2 is 17 to the end. I was thinking of ccombining them for neatness.

    Also how do you force the www. to be prepended?

  6. Andy Beard (1685 comments.)
    Posted April 25, 2007 at 10:11 pm | Permalink

    If you use WWW it takes up more space – someone can type in www and it will work, and just redirect to the shorter version.

    Alister has some usable code listed on his page, but that doesn’t automatically set the host, but if you are not running lots of blogs, that isn’t a major issue.

  7. Philip de Lisle (4 comments.)
    Posted April 25, 2007 at 10:46 pm | Permalink

    You might want to check out the Portal Feeder blog at http://portalfeeder.com/blog/?p=54 as this seems, to my untrained eye, to do even more

  8. Alex (2 comments.)
    Posted May 26, 2007 at 1:02 am | Permalink

    Hehe now you need a write up for us lighttpd users ;)

  9. Placement papers (1 comments.)
    Posted June 26, 2007 at 10:58 am | Permalink

    Nice Post, Here is what I am using to fight against canonicazation in wordpress (thanks to http://www.jimwestergreen.com for the code)
    [code]
    Options +Indexes
    Options +FollowSymLinks
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^myblog\.com
    RewriteRule ^(.*)$ http://www.myblog.com/$1 [R=permanent,L]

    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} !(.*)/$
    RewriteRule ^(.*)$ http://www.myblog.com/$1/ [L,R=301][/code]

  10. Athlete Fuel (1 comments.)
    Posted July 4, 2007 at 10:07 am | Permalink

    Thanks Andy! This is a great post. Time to go tinker with my .htaccess file.

  11. AskApache (2 comments.)
    Posted July 12, 2007 at 6:35 am | Permalink

    Very interesting idea! You could definately get answers at htaccesselite.com’s htaccess forum..

  12. Posted August 20, 2007 at 6:12 pm | Permalink

    [code]# If subdomain www exists, remove it first
    RewriteCond %{HTTP_HOST} ^www\.([^\.]+\.[^\.]+)$ [NC]
    RewriteRule ^(.*)$ http://%1/$1 [R=301,L] [/code]
    The code above is for no-www
    How to code for yes-www? (I’m a newbie :P )

    I tried some code but it requires entering url.

  13. Posted September 29, 2007 at 10:13 am | Permalink

    Thanks Andy for this post. Just the thing I needed. (even more and better than I was searching for!)

  14. Wan Su (1 comments.)
    Posted October 10, 2007 at 7:26 am | Permalink

    hi all, do i have to backup my WP database before doing this ?

  15. uGuX (3 comments.)
    Posted November 26, 2007 at 5:14 pm | Permalink

    Wan Su,

    You don’t have to backup the WP database.

  16. Vacation Rentals (1 comments.)
    Posted December 27, 2007 at 7:56 am | Permalink

    Just curious why you want to get rid of the trailing slash? Trailing slashes are definitely the way to go. It takes longer for a server to render a page with them off. It’s one more iteration it has to go thru…

    • Andy Beard (1685 comments.)
      Posted December 27, 2007 at 10:42 am | Permalink

      now reworded – the code gets rid of trailing slash problems

      You will notice I don’t use trailing slashes on this site for canonical permalinks, because I have a specified document type.
      If you don’t have a specified document type, with folders being used as a permalink URL, then you would use a trailing slash – that works too.

  17. Igor The Troll (123 comments.)
    Posted December 27, 2007 at 10:29 am | Permalink

    Andy, ..:)

  18. joomla guy (1 comments.)
    Posted May 29, 2008 at 12:27 am | Permalink

    Ive been hacking my brain on figuring out the best way to set up my htaccess file for about a week now and nothing seemed to bear fruit. Just tried your recommended approach as outlined above and Voila! it works like a charm :)

    Thank you!

  19. rgopinath
    Posted July 22, 2009 at 11:05 am | Permalink

    Hi I am cracking my head over here.
    Still i am not able to set permalinks. I am using plesk control panel.

    http://blogs.neuronring.com

  20. Nokia 5530 Blog (1 comments.)
    Posted September 3, 2009 at 12:01 am | Permalink

    I have the following .htacess file, working fine at site root:
    RewriteEngine On

    #if the requested filename does not exists (as file nor directory), then assume CAT_NAME_HERE/SUB_CAT_HERE

    RewriteCond %{REQUEST_FILENAME} !-f

    RewriteCond %{REQUEST_FILENAME} !-d

    RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?c=$1&area=$2 [L,QSA]

    RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /index.php?c=$1&area=$2&d=$3&subarea=$4 [L,QSA]

    ###END################################…

    But now I instaled wordpress at a subfolder like http://www.website.com/worpress/ and

    with this .htaccess file the images of subfolders like /wp-admin/ do not display.

    Thank your for your time.

  21. AndyBeard (150 comments.)
    Posted September 3, 2009 at 12:37 am | Permalink

    Basic rewites, just let Wordpress do it

  22. Andy Beard (1685 comments.)
    Posted June 20, 2007 at 1:16 am | Permalink

    If it is not working for you, give Alister’s code for the slashes a try

18 Trackbacks

  1. By Around the Net II 3/3/2007 | knupNET on April 3, 2007 at 3:06 pm

    [...] Ultimate Htaccess file. [Andy Beard] [...]

  2. [...] from Google’s Blog Search (Patent Application) Being Scraped? Here is Something You Can Do The Ultimate Wordpress Htaccess File? Organize your pdf library with [...]

  3. [...] your blog land on a single front index page. I spotted the code and other interesting things at Niche Marketing – Andy Beard which took to me to Alister Cameron // Blogologist where I was able to read even more. Both of [...]

  4. [...] to give where credit is due so the point of reference for the code is from Andy Beard’s blog Andy Beard’s Ultimate Answer to Canonicalization Problem. In it he goes a little more in-depth and explains the code for those of your who are interested. I [...]

  5. [...] Do yourself a favor and checkout LinkVendor to help you make search friendly websites & Andy Beard’s resource on .htaccess for [...]

  6. [...] Here’s a few useful reference links. [...]

  7. [...] There are a couple of more SEO tips that we can plug into that .htaccess file that wordpress auto-created when you made your pretty permalinks. Andy Beard has created what he calls the Ultimate Wordpress Htaccess File. [...]

  8. [...] hunted down the problem. I’ve started using a Wordpress specific .htaccess file that I found here, which has some nifty optimizations that should help general browsing. Dreamhost, my web host, was [...]

  9. [...] If you have chosen to use www, then just include the second half of the code from my post about Wordpress htaccess to cover the trailing [...]

  10. By Showing A Little Geek Love « Geek News on July 5, 2007 at 1:06 am

    [...] Andymatic:Hey, who doesn’t love the name Andy? Seriously, Andymatic is a personal blog with a well rounded topic list of links and bookmarks covering politics, tech news and pop culture. For instance, he found a really cool blog link to ‘The Ultimate Wordpress htaccess file’ [...]

  11. [...] Beard has a very important post about the ultimate.htaccess file. I have been looking for something that would work for redirecting all pages to “www.” [...]

  12. [...] it for a while. I finally did fix it, by putting the right cryptic code into the .htaccess file. This site helped me pick what to put in it, and with some tweaking I finally got this site back up. All I [...]

  13. By Optimized Permalinks For Wordpress on August 1, 2007 at 2:05 pm

    [...] posts in your mailbox thingamajig. That would be awesome!As I was reading the tutorial about the ultimate .htaccess of andy, I couldn’t help but notice that his permalinks are not only looking sexy, it also has nice [...]

  14. [...] folgendem Blog Beitrag (The Ultimate Wordpress htaccess File) findet man eine sehr gute und ausführliche Anleitung [...]

  15. By Mellow Morning » Setting up .htaccess for Wordpress on September 5, 2007 at 7:38 pm

    [...] reading the great mod_rewrite documentation and combining that with a great .htaccess files here my [...]

  16. [...] You can get the details about how to do this from Andy Beard’s excellent article “Ultimate WordPress Htaccess file” or from Sebastian’s Pamphlet on trailing slash canonicalization . Be careful with your [...]

  17. By Importance of Backing Up Blog Regularly on August 8, 2008 at 3:15 pm

    [...] write some rules in .htaccess file (mod_rewrite). Hence while trying to do that as per directive of ultimate .htaccess by Andy Beard and per directives of no-www.org to make this blog as class B and per Daily Blog Tips on 301 [...]

  18. [...] out it was something to do with the .htaccess file.  Thanks to Andy Beard, who unwittingly saved my day. Comments 0 Post Comment [...]