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 – October 17, 2007
eddie,
Given your stated objectives, your method seems ideal. Although I have yet to experiment with file extensions in permalinks, WordPress should function perfectly well with your stated format. To my knowledge, I don’t recall encountering anything that would suggest otherwise. I would definitely be interested in hearing about your experience with such an implementation as well.
Regards,
Jeff_
Terrie – November 12, 2007
Thank you for your insight into word press 404 error pages and the permalink structure! I was lost but now I am back up functioning perfectly.
Perishable – November 13, 2007
You’re welcome – thanks for the feedback! ;)
Not Impressed By Much – November 27, 2007
You are the absolute best! This is the fix of all fixes! I owe you a million!
Perishable – November 28, 2007
I love it! Thank you so much for the generous feedback! ;)
Marcus Hochstadt – December 23, 2007
Hi James,
Boy am I glad I found your blog. :) James, you are my hope…
I tried it with your great instructions above, to no avail though. I always get the same error message; that is,
“Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.”
This happens whenever I change the permalink settings. Once I click on that “Update Permalink Structure” button the browser displays the above message.
That means, I’m then unable to access anything, the site is unaccessible, until I delete the .htaccess file from the root folder (where the blog is located.)
In other words, I installed WordPress 2.3.1 and it seems I cannot modify the permalink structure.
Do you think it can be a problem with my Web host? Although they clearly state I can use a WordPress blog on my site and even offer a one-click installation. Or perhaps I’ll have to indicate a subdirectory that exists on the server for my specific domain, somehow? (like ‘public_html’, which didn’t work either though)
Any thoughts?
My best,
Marcus
P.S. Marry Christmas! :)
Perishable – December 26, 2007
Hi Marcus,
Good to hear from you ;)
Assuming you are paying attention and following all the required steps, this sounds like a hosting/server-related issue. I would try contacting them and explaining the situation, just as you have done here. If they are reasonably decent, they should be able to troubleshoot from their end and meet you in the middle with a possible solution.
Otherwise, if the problem lands back in your court, you may want to try setting up a temporary test blog and see if you can get permalinks working there.
Good luck!
Adrian – January 2, 2008
Thanks so much for this post, it helped me greatly in understanding how wordpress implements permalinks under the hood. I installed wordpress under the root directory of my site and realized with mod_rewrite that any other sub-directories off of root that I previously had used would not work…and instead be routed through wordpress and eventually the error page. Is there any way to add exclusions for subdirectories? Thanks for any help.
Perishable – January 2, 2008
Hi Adrian,
I am a little confused by your question.. are you saying that you have additional installations of WordPress in subdirectories while also running a root installation of WordPress? Otherwise, what type of subdirectory content is being processed by the root rewrite rules?
Regards,
Jeff
Adrian – January 2, 2008
No, only one installation of wordpress, I just installed it in the root www folder with my webhost. I had other subfolders outside of wordpress I was using for various things. With the rewrite rules on, I can’t access those subfolders anymore, since wordpress picks it up as an error in what it believes to be a permalink. So I was wondering if there was a way to exclude certian folder names from the rewrite rules. Thanks again, I haven’t found any info on wordpress installations with these requirements so I really appreciate your help.
Perishable – January 2, 2008
Ah, okay.. Thanks for clearing that up for me. Have you tried disabling the rewrite rules in your non-WordPress subdirectories? Create an htaccess file for each folder and add the following rule:
# disable the rewrite engineRewriteEngine offAnd that should do it. After adding that, WordPress permalink rules should not effect the contents of the non-WP subdirectories.
Adrian – January 2, 2008
Thank you! That worked! I was concerned doing this would mess up my sub-domain setup because for some reason the sub-domain info looked like it was in the rewrite rules of the subfolder in question. But I removed the rewrite rules from the subfolder, and my sub-domain still worked, this time without getting the wordpress error!