Update 2012/07/15 all code updated with the new .htaccess rules (changed in WP 3.0). The code in this article should work with all versions of WordPress. </update>
I recently performed a series of tests on a fresh installation of WordPress 2.8.6 to determine the exact htaccess rewrite rules that WordPress writes to its htaccess file for various permalink configurations. Under the WP admin option menu, WordPress lists four choices for permalink structure:
- Default:
http://perishablepress.com/press/?=123 - Date and name based:
http://perishablepress.com/press/index.php/2006/06/14/sample-post/ - Numeric:
http://perishablepress.com/press/index.php/archives/123 - Custom:
/%year%/%monthnum%/%day%/%postname%/
The "default" option is to not use permalinks. The "date and name based" setting invokes the /index.php/%year%/%monthnum%/%day%/%postname%/ rule pattern. The "numeric" setting invokes the /index.php/archives/%post_id% rule pattern. And the "custom" setting invokes whatever pattern is specifically chosen. For our "custom" option, we chose the /%category%/%author%/%postname%/ pattern.
For the test, we began with the common "date and name based" permalink configuration. Then, after invoking the chosen permalink structure, the htaccess file was downloaded and the new WP rules were recorded. After this, the admin permalink settings were switched back to the "default" configuration. Finally, we deleted the new rules and uploaded the htaccess file. This process was diligently repeated for each different permalink configuration. It should also be noted that blog performance was checked during each round of testing.
The results indicate conclusively that WordPress uses the exact same set of htaccess rules for all permalink configurations. Surely this information is available elsewhere on the internet, however we were experiencing several inconsistencies related to permalink structure that inspired us to determine for ourselves the precise htaccess rules for WordPress permalinks.
Without further ado, the htaccess rules for all WordPress permalinks1 are precisely either #1 or #2:
[ #1 ] If WordPress installed in the root directory »
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
[ #2 ] If WordPress installed in a subdirectory called "foo" »
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /foo/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /foo/index.php [L]
</IfModule>
# END WordPress
1 Note: These results are valid for standard permalink structures invoked via a standard WordPress install and may not operate effectively for non-standard or highly specialized configurations. We assume that if you are hard at work hacking and tweaking, you must know what you are doing.
96 Responses
Soni – September 17, 2007
Wow, thanks for this. I’m setting up a new blog and couldn’t figure out why the permalinks tweak wasn’t working. Turns out, it was the subdirectory path in the htaccess file was missing.
Man, I’ve been pulling my hair out for a couple of hours now trying to figure this out and all it took was one side-by-side glance from my file to your examples to highlight the problem. I just wish I’d tumbled over this site sooner, rather than trying to weed through pages of High Geek on the WP forums. (I speak only Low Geek, I’m afraid) :-D
You rock.
Perishable – September 18, 2007
Thanks for the feedback, Soni — I am glad that you found the information useful. As a proud member of the “Low Geek” Society, I too found the convoluted maze of High Geek documentation far too tedious for any serious attempt at implementing something as important as WordPress permalinks. May the overlords of Low Geekdom bestow a million visitors upon your site!
Rickart – September 20, 2007
Fast search, fast answer, fast solution. Thank you so much !
Perishable – September 22, 2007
You are very welcome, Rickart!
ion – September 29, 2007
Hi,
I have reached your website looking for a solution with my Permalinks. I have just installed my blog and when I change the setip for a more prettty permalinks, the .htaccess file is created as your example above. The problem is I cannot access my blog anymore I get the 403 Forbidden Access message. If I delete the htacces file, it works again.
Do you have an idea what am I doing wrong.
Thanx in advance
Perishable – September 30, 2007
ion,
Two questions:
1. Which version of WordPress are you using?
2. Is your blog installed in a subdirectory?
kym – October 11, 2007
I suspect there is something here which can resolve the problem I have been unable to resolve but I’m not sure how to use the information exactly. Everything on my site works, except for pagenav whether it be at the bottom of the main page, an archives page, etc. When you click on one of the page numbers it fails with a 404 because the url has an extra /blog in it. If that were not there it would work. I have tried varies permalink settings and still no good. I found I did not have an htaccess file so I created on and put the string of commands in it that wordpress gave me and made sure the permissions on the file were ok. I tried it in a few different subdirectors. No good. Have you any recommendations for me based on what you have learned regarding wordpress filestructure rules and the htaccess file or anything else really. I’ve been at this for months and it’s making me crazy.
Thanks in advance for any advice.
Perishable – October 11, 2007
Hi kym,
I suspect this has something to do with the plugin used to generate the page links. May I ask which plugin you happen to be using?
Regards,
Jeff
kym – October 12, 2007
Oh I’m sorry – I should have said in my original bit of information that this problem occurs no matter what plugin (because I have tried a few hoping it fixes the problem) I use or if I use none at all. I just deactivated the plugin so you can see what I mean. When the page is assembled that /blog is already in there. I should also have said that the wp install is in a sub directory and not htdocs. I’ve thought that to be the root cause of the issue but assumed there was some way to work around it which is why I thought, when I read this thread, that perhaps something about the htaccess file and wp rules would make a difference.
Perishable – October 15, 2007
kym,
I would recommend scouring your blog for key instances of the word “blog”. For any WordPress-powered site, there are two fundamental components: the database and the physical files (
.php, etc.). Assuming that you have closely inspected your htaccess file for any extra instances of the term “blog”, the next thing to do would be scour the database. Using phpMyAdmin (or whatever), log into your WordPress database and execute a search for “blog” on all tables. Depending on the size of your database, you will get the phone book in return, but with a few logical eliminations, you should be able to narrow the search down to one or two target tables. For example, it probably is not necessary to investigate any occurrences of “blog” that are found in anti-spam plugins, feed-reader plugins, and other peripheral plugins; on the other hand, you will definitely want to examine any hits from your options table and any other core WordPress tables containing the term. Likewise, you should also do a comprehensive search of your site files for the target term, and check out each and every result. The point is, that the extra “blog” that is being inserted into the navigation routine has to be coming from somewhere (duh), and so it is just a matter of tracking it down. It may have been 100 times easier to locate a word that is not nearly as common as “blog”, but we work with what we have been given ;) Anyway, I hope this has helped in some way. I realize I am making all sorts of assumptions here, but I am only trying to help ;) Good luck!kym – October 15, 2007
I have scoured the files that are available via the admin, edit themes interface but not gone into the server and looked at everything there. I didn’t think about that. I will try it. I should also note that I didnt have an htaccess file. I created one because I’d hoped creating one with the right stuff would fix my problem. Maybe I’ll look into that a little deeper.
I greatly appreciate your help. Thank you for taking the time! If I sort it out I’ll let you know. Thanks again.
eddie – October 16, 2007
jeff, i’ve seen a lot of articles but yours is one of the best resources on the topic.
i found it because i am moving from MT to WP. i have mt using this permalink format
/mt/archives/postname.htmlusing wp-admin, i set the custom permalink in WP to
/mt/archives/%postname%.htmli will use htaccess to convert dashes to underscores that are used in lieu of spaces in the post names.
keeping in mind the fact that i am a WP newb, it seems to be the simplest way to guarantee that my google SERPS won’t get dropped… i don’t even know why i would do it another way.
my question is: do you see a problem with doing it this way?
thanks