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
Perishable – April 8, 2008
Hi Russell,
It has been awhile since I have dealt with this issue, but if I remember correctly, the trick with WordPress URLs involves using permalinks to get around Apache’s miserable support for query strings. Once you have permalinks enabled, the htaccess code given in your comment should work just fine. I think..
Scott Fasser – May 13, 2008
Thanks for the good info. I have a problem though. I used version #1 above and was able to generate a link to page from a blog post with the right URL structure, but the content is not being pulled. Can you take a look and tell me what I’m doing incorrectly?
Perishable – May 13, 2008
Hi Scott,
Without proper access, it is difficult to diagnose for certain the cause of the issue. Based on the URLs having been rewritten, it appears that the htaccess code is working correctly. This leads me to question whether the permalink structure has been defined in the WordPress Admin area. Unless you specify the desired format via WordPress, the htaccess rules will direct the links to nowhere. I hope that helps — check it out and let me know how it goes..
Ben E – May 17, 2008
This info saved me. It is the only page I found that gives the correct .htaccess code AND indicates that the name of the WP subdirectory (if you have one) MUST be in the .htaccess file EVEN if the file in in that subdirectory. Finally I have an rss feed.
However, is is /?feed=atom. How can I change it to feed=rss? (Do I really needto change it?)
Perishable – May 18, 2008
Thanks for the positive feedback, Ben. It is great to know that this article continues to help people with their WordPress permalink configuration.
As for the location/URL of your WordPress-generated feed(s), perhaps this article will shed some light on the issue :)
Kirrus – June 15, 2008
I was setting up a word-press comic for a friend and it didn’t set the .htaccess for me, as my host is paranoid about permissions… Thanks for the invaluable gem here :)
The site, if you’re interested, is here:
http://www.blue-world.co.uk/jaydenandcrusader/
Perishable – June 17, 2008
My pleasure, Kirrus — thanks for the positive feedback! Best of luck to your friend’s site ;)
Rich – June 19, 2008
So I am trying to setup WP 2.5.1 with Permalinks. I have done this before with 3 different blogs so I figured I know what I am doing. Of course, they are on a different host so…
Anyway, I set AllowOverride All in the httpd.conf file, then setup the .htaccess correctly (checked it 3 times), setup permalinks, and made a new post. Didn’t work.
On the server we use Apache and a *nix OS (not sure which one since I didn’t set it up).
Any insights would be appreciated. And thanks for the wonderful article that made it all clear.
Rich – June 19, 2008
Never mind, I dug a little deeper and realized that whoever setup the httpd.conf file left the default directory entry in there and did not put the actual one in. Once I did that and restarted Apache it worked. :)
Perishable – June 22, 2008
Glad to hear you got it working, Rich! :)
Mike – June 24, 2008
Hi,
All of the above information is great, but I have a slightly different problem:
On of my sites has a WordPress blog in the root of the domain, and another website/script in a sub-folder that works with an index.php file.
As a result the pages in the sub-folder are displayed as a WordPress 404 page.
I have tried turning the Rewriteengine off in the sub-folders htaccess, but that doesn’t seem to help as I guess the WordPress rewrite rule precede and override it in the folder structure.
Is there a way to modify the WordPress htaccess so that it ignores certain folders?
Perishable – June 24, 2008
Hi Mike, have you seen this article on allowing hotlinking in specific directories? That article provides some excellent techniques that should apply to the WordPress
Rewritedirectives. By simply adding this to your subfolder’s HTAccess file:# disable hotlink protectionRewriteEngine off..the WordPress permalink rules should be disabled for all content within that subdirectory. Good luck!