Latest TweetsDifference between mod_alias and mod_rewrite perishablepress.com/difference…
Perishable Press

The htaccess Rules for all WordPress Permalinks

Updated June 30th, 2016: All code current with WordPress 4+. The permalink rules in this article should work with all versions of WordPress.</update>

I recently performed a series of tests on a fresh installation of WordPress to determine the exact .htaccess rewrite rules that WordPress writes to its .htaccess file for various permalink configurations. In the WordPress General > Permalinks settings, WordPress lists six options for permalink structure:

Plain              http://example.com/?p=123
Day and name       http://example.com/2016/06/30/sample-post/
Month and name     http://example.com/2016/06/sample-post/
Numeric            http://example.com/archives/123
Post name          http://example.com/sample-post/
Custom Structure   http://example.com/%postname%/

These default permalink options employ the following Structure Tags:

Plain              (permalinks disabled)
Day and name       /%year%/%monthnum%/%day%/%postname%/
Month and name     /%monthnum%/%postname%/
Numeric            /archives/%post_id%/
Post name          /%postname%/
Custom Structure   (user-defined structure)

You can read more about WordPress permalinks at the WordPress Codex.

Testing

For the test, we began by enabling the “Day and name” permalink configuration. After saving our changes, we downloaded the .htaccess file that is located in the root directory of our WordPress installation:

/wordpress/.htaccess

We then copied the rules and recorded them in this post (see next section). To verify that the .htaccess rules are the same for all types of permalinks, we tested each of the different permalink options and compared the results. Further, we tested the site for proper functionality during each round of testing.

Once we had determined the correct permalink rules for WordPress installed in its own directory, we repeated the entire test with WordPress installed in the site’s root directory. This test revealed that the permalink rules are different depending on the location of the WordPress installation. Otherwise, the rules are identical, as explained below.

Results

So after much testing, we conclude the following results:

  • Given any WordPress installation, the .htaccess rules are identical for all permalink options.
  • The .htaccess rules only differ when WordPress is installed in its own directory vs. WordPress installed in the site’s root directory.

Surely this information is available elsewhere online, however we were experiencing several inconsistencies related to permalink structure that inspired us to determine for ourselves the precise .htaccess rules for all WordPress permalinks.

The htaccess Rules for all WordPress Permalinks

Without further ado, the .htaccess rules for all WordPress permalinks1 are either of the following, depending on where WordPress is installed.

1) If WordPress is installed in the site’s 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 is installed in its own directory, /wordpress/:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /wordpress/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wordpress/index.php [L]
</IfModule>
# END WordPress

Notice the only two differences between these two sets of rules: the value of the RewriteBase and RewriteRule directives. Specifically, when WordPress is installed in the site’s root directory, the value is /. Otherwise, if WordPress is installed in its own directory, the value is the name of the directory, which is /wordpress/ in this example.

Footnotes

1 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.

Jeff Starr
About the Author Jeff Starr = Creative thinker. Passionate about free and open Web.
Archives
96 responses
  1. Jeff Starr

    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..

  2. Scott Fasser May 13, 2008 @ 1:45 pm

    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?

  3. Jeff Starr

    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..

  4. 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?)

  5. Jeff Starr

    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 :)

  6. 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/

  7. Jeff Starr

    My pleasure, Kirrus — thanks for the positive feedback! Best of luck to your friend’s site ;)

  8. 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.

  9. 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. :)

  10. Jeff Starr

    Glad to hear you got it working, Rich! :)

  11. 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?

  12. Jeff Starr

    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 Rewrite directives. By simply adding this to your subfolder’s HTAccess file:

    # disable hotlink protection
    RewriteEngine off

    ..the WordPress permalink rules should be disabled for all content within that subdirectory. Good luck!

[ Comments are closed for this post ]