Book Sale! Save 20% on WordPress books with discount code: SAVE20
Web Dev + WordPress + Security

The .htaccess Rules for all WordPress Permalinks

Updated January 28th 2021: All code in this article is current with WordPress 5 and better. The permalink rules presented below should work with all versions of WordPress. That is, the current rules are backward compatible. </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 WordPress.org.

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

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
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

2) OR, if WordPress is installed in its own directory, /wordpress/:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
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. So if you have WordPress installed in its own directory, make sure to change /wordpress/ to the actual directory name.

Update

WordPress now (finally) has an official page about .htaccess at WordPress.org. There you can find the same permalink rules that are provided above. If they are different in any way, please let me know so I can update the article. Thank you.

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.

2 Note that WordPress also adds several of inline comments when it automatically adds its permalink rules to your .htaccess file. The inline comments look like this:

# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.

The comments are included after the # BEGIN WordPress line. Because they are comments (i.e., begin with a pound sign #), they are ignored by Apache and have no effect on anything.

Jeff Starr
About the Author
Jeff Starr = Creative thinker. Passionate about free and open Web.
USP Pro: Unlimited front-end forms for user-submitted posts and more.

96 responses to “The .htaccess Rules for all WordPress Permalinks”

  1. Jeff Starr
    Perishable 2007/10/17 7:55 am

    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_

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

  3. Jeff Starr

    You’re welcome – thanks for the feedback! ;)

  4. Not Impressed By Much 2007/11/27 3:22 pm

    You are the absolute best! This is the fix of all fixes! I owe you a million!

  5. Jeff Starr

    I love it! Thank you so much for the generous feedback! ;)

  6. Marcus Hochstadt 2007/12/23 3:39 pm

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

  7. Jeff Starr

    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!

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

  9. Jeff Starr

    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

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

  11. Jeff Starr
    Perishable 2008/01/02 4:50 pm

    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 engine
    RewriteEngine off

    And that should do it. After adding that, WordPress permalink rules should not effect the contents of the non-WP subdirectories.

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

Comments are closed for this post. Something to add? Let me know.
Welcome
Perishable Press is operated by Jeff Starr, a professional web developer and book author with two decades of experience. Here you will find posts about web development, WordPress, security, and more »
WP Themes In Depth: Deep dive into WP theme development.
Thoughts
Currently having a blast redesigning Plugin Planet, so much work so little time.
Thanks to David McCan over at WebTNG for the awesome BBQ Pro review.
Enjoyed a nice mini-vacation with my fam. Great way to recharge and regroup.
Nice little interview with yours truly over at ThemeIsle. WordPress, web dev & more!
Perishable Press celebrating 16 years online! An incredible, rewarding journey.
Thanks to Nicholas Ferrell for his excellent and thorough review of Wutsearch search-engine launchpad.
Great article about using RSS as a Facebook alternative. I use RSS/feeds for news every day. Facebook, not so much.
Newsletter
Get news, updates, deals & tips via email.
Email kept private. Easy unsubscribe anytime.