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
Jeff Starr – November 10, 2008
@Laurent: case-sensitivity in
mod_rewriteis determined by the presence or absence of theNC(No Case) flag for any of the variousRewriteCondorRewriteRuledirectives.Check your rewrite rule (the last line in the
IfModulecontainer). By default, WordPress rewrite rules are case-sensitive, so there is noNCflag specified for theRewriteRule. To make the default WordPress permalink rewrite rules case-insensitive, you may add the flag like so (again, last line in theIfModulecontainer):RewriteRule . /index.php [NC,L]Keep in mind that this is a hypothetical modification we are discussing here; I have not tested this method on any live site, but the logic behind it is very straightforward and should work just fine out of the box. I hope this helps!
jimsa – November 10, 2008
Hi Jeff
First off, great site and excellent feedback to various comments!
I am having a weird (possibly stupid) problem… I have an install of WP in /site folder in the root my site.
I put .htaccess #2 from your example above into /site (changed the two refs to foo) and removed .htaccess file from the root.
I then selected ‘Month & Name’ from on the Permalinks page. The index shows up fine but links throught to pages and posts aren’t working, I get a 404 instead.
Weird thing is that I have other installs of WP within other folders behaving perfectly well!
Any idea what is happening?
Many thanks in advance.
j.
Laurent – November 10, 2008
Thanks Jeff for the info on the case-sensitivity. All these flags and rules are giving me a headache, I’m thinking of asking my programmer friends to create a simple program to auto generate .htaccess files based on the options selected/needed, or is there one already? Studying .htaccess rules is like learning a new language. :)
Jeff Starr – November 16, 2008
@jimsa: hi, are you still experiencing the issue? If so, would you mind providing a link to the blog/site in question? I wouldn’t mind having a look..
@Laurent: No problem, I completely understand about htaccess headaches! ;) As for an application to automatically generate htaccess directives, I have encountered several on various sites online.. try a search for something like, “htaccess generator” via Google and you may find something that does the trick!
heckennedy – December 1, 2008
Woum thanks!, now i can access to my page.
Anak Balita – December 2, 2008
this very helpful for me a few hour ago I try to move my blog from
http://balitabiz.co.cc/tohttp://balitabiz.co.cc/blog/but after that i can’t access my setting. and then i looking for the default setting htaccess for wp, after looking for this tutorial for hour a find this…THANKS A LOT i know this wp default htaccess not the greatest htaccess but for newbie like me this enough for this time.
ones again thank’s
jorge orejel – December 3, 2008
hola quiero instalar varios subdirectorios con wordpress
ejemplo:
www.payastitlan.com.mx/www.payastitlan.com.mx/pestanin/www.payastitlan.com.mx/prueba/y utilizar en todos los Permalinks.
¿como configuro el htaccess?
Saludos y gracias.
——
hey I want to install several subdirectories with wordpress
example:
www.payastitlan.com.mx/www.payastitlan.com.mx/pestanin/www.payastitlan.com.mx/prueba/and use in all Permalinks.
How do I set htaccess?
Greetings and thanks.
Jeff Starr – December 8, 2008
@jorge orejel: what permalink structure were you hoping to achieve for those subdirectories? Their URLs seem pretty solid simply “as-is”, with no extra modifications necessary. They should work fine by simply linking to them, I would think..
Nick – December 19, 2008
Thanks SO much! This has helped me vastly.
Justin Foerster – December 22, 2008
Holy moly, I was tearing my hair out yesterday trying to solve this. Funny, I found this page yesterday and tried to do your instructions and it didn’t work.
Slept on it, and just tried it and … Eureka. Beautiful permalinks without 404 errors.
Thank you so much!
Jeff Starr – December 31, 2008
@Nick, @Justin Foerster: Thanks for the positive feedback! Happy New Year! :)
Janay Green – March 7, 2011
I have a Zen Cart and I just added WordPress. I wanted to say that if you have a Zen Cart and you currently have rewrite rules for one of your mods, then you will need to put the rewrite rules for WordPress in your WordPress directory (mine is called
/blog/) That way, your domain will point to your online shop and not your blog.