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
Mac – August 9, 2006
Thank you. You have no idea how helpful this was. A site I recently became responsible for (not my own) was hosed by wp’s htaccess shenanigans. Thanks posting.
Perishable – August 15, 2006
Happy to help — thanks for the feedback!
gices – January 16, 2007
Wow, i never knew all permalinks structure had the same htaccess rules. Thx 4 the info!
Perishable – January 16, 2007
My pleasure!
Michael Fitzpatrick – May 17, 2007
Yet another thank you for putting this together!!! Chalk me up as another happy user of your knowledge-share.
Peter – June 9, 2007
Hi,
Thanks for the tutorial. My .htaccess file follows the form of #1 above, but I am unsure how to setup permalinks to pages in wordpress, not posts. Is there a specific way of doing this? Much appreciated.
Peter Dowse – August 8, 2007
Hi
Thank you so much for publishing this article. This helped me to upload the correct htaccess file to my server and get kick arse SEO friendly permalinks working.
Thank you so much….I have been looking for days on the net and this is the only site that has answered my questions AND shown me how to get the proper code in my htaccess file.
Cheers guys….thanks very much!
Perishable – August 13, 2007
You are very welcome! Thanks for the feedback!
Michael – September 4, 2007
I’m not much of a coder and also am relatively new to WordPress and blogging.
When setting up a custom permalink last night WP wrote to my .htaccess – both in my root AND blog folder – as in example #2 above.
My blog is installed off the html root in a folder called – blog.
The WP entry in the root .htaccess prevented me from publishing my website. The entry in the /blog sub-folder I’m wondering if that should be as in example #1 above or, just leave it as is as in example #2?
When I totally removed the WP permalinks entry from the root .htaccess then I could connect and publish to my website again ok.
If my blog is in a subfolder called blog with its own .htaccess then should the WP entry have the subfolder name /blog in there or not?
Thank you.
Perishable – September 4, 2007
Michael,
When WordPress is installed in a subdirectory, the root htaccess file does not need any WP-related rewrite rules.
The subdirectory htaccess file does need rewrite rules if permalinks are to be used. In this case, the name of the subdirectory must be included in the rewrite rules.
Thus, adapt the code from case #2 and place it into the htaccess file located in the subdirectory containing your blog. Nothing else should be needed..
Regards,
Jeff
Michael – September 6, 2007
Thanks a lot for your help and quick response Perishable. I appreciate that. I’m beginning to feel like an htaccess expert now :)
Cheers. and keep up the good info. in the blog. We all appreciate that.
Perishable – September 8, 2007
My pleasure, Michael — glad to be of assistance ;)
Cheers!