Spring Sale! Save 30% on all books w/ code: PLANET24
Web Dev + WordPress + Security

The .htaccess Rules for all WordPress Permalinks

Updated January 15th 2023: All code in this article is current with WordPress 6 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 following2, 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.

About the Author
Jeff Starr = Web Developer. Security Specialist. WordPress Buff.
The Tao of WordPress: Master the art of WordPress.

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

  1. Thanks Jeff! I look forward to the solution. :)

    Anyway awesome site you have here, lots of technical info that I wouldn’t find anywhere else. You got one more loyal reader to your site. Thanks.

  2. Hi I had an Internal Server Error when I edited or add the .htaccess on my wordpress directory. I found out a solution from the Internet. Add the following on the first line of your .htaccess file..

    Options +FollowSymLinks

    That works for me.

    Chetz Yusof

  3. Yes symbolic links must be enabled on your server in order for the rewrite rules to take effect. In my experience, most servers have this option enabled by default, but some do not. Overriding the default setting via htaccess as you have done here is a sound solution. Another good idea is to wrap the WordPress rewrite rules in an IfModule container to check for the presence of the required functionality. In any case, thanks for sharing, Chetz — much appreciated!

  4. You’re most welcome Jeff! I know how frustrating it is to figure out something you don’t know anything about. So, I hope by sharing this info. it’ll help someone.

  5. Thank you so much for the information.

    I used your code for WP install in root above verbatim.

    i have my permalinks set to: /%postname%/

    My question Is, why do links to pages that are not assigned a page parent (main nav items) not work; however, pages that are assigned to that page parent previously listed do display?

    In other words:
    Not working – ./about/
    Working – ./about/test2/

    thank you in advance for your response.
    Silver

  6. Jeff Starr 2008/10/05 2:12 pm

    Hi Silver, I am not sure what the issue could be, but you may want to try resetting your permalinks and then re-creating them. Reset them back to WordPress defaults and then delete the WordPress htaccess rules from your htaccess file. Then surf around on your site for awhile to ensure that each of the different types of pages are loading correctly (via the default URL structure). If everything looks good, try enabling your desired permalink format and then replacing the htaccess rules provided in this article, which should be the same as the ones generated by the WordPress admin panel, btw. Hopefully, that will do the trick; but if not, there may other issues involved. I hope that helps!

  7. Those .htaccess rules do not fix non-www and www duplicate content issues.

    That is a vital thing to add to the mix.

  8. Jeff Starr 2008/10/06 8:23 am

    @g1smd: You are correct! This article focuses on the htaccess rules for all WordPress permalinks (as the title suggests). I deal with WordPress canonicalization issues separately in other articles. Here are two of my finest efforts, but there are certainly others available via the “Related articles” section at the end of each post.

    Comprehensive HTAccess Canonicalization for WordPress.

    htaccess Combo Pack: WordPress Permalinks and non-www Redirect

    I hope that helps! Cheers!

  9. Holy crap, you just saved my A$$! Thank you!

  10. I have been trying to figure this pretty permalink thing out with no success. I implemented your #2 solution. The IT guys installed the mod_rewrite.so instead of mod_rewrite.c. Should this make any difference? I’m pretty new to .htaccess thing.

  11. Jeff Starr 2008/11/02 9:03 am

    Hi Mark, I am not sure what the issue could be, but a search on Google reveals quite a few questions/concerns about mod_rewrite.so-related issues. I would recommend speaking directly with your IT people; there are many things that they could have done to affect Apache’s rewrite functionality.

  12. Hi all, regarding my comment #71 I have found the problem. It was a case-sensitive problem. One of my files had an uppercase first letter and once I change it to lowercase through ftp, it worked perfectly. Does htaccess rules look at case sensitivity? A bit confused here…

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 »
SAC Pro: Unlimited chats.
Thoughts
I live right next door to the absolute loudest car in town. And the owner loves to drive it.
8G Firewall now out of beta testing, ready for use on production sites.
It's all about that ad revenue baby.
Note to self: encrypting 500 GB of data on my iMac takes around 8 hours.
Getting back into things after a bit of a break. Currently 7° F outside. Chillz.
2024 is going to make 2020 look like a vacation. Prepare accordingly.
First snow of the year :)
Newsletter
Get news, updates, deals & tips via email.
Email kept private. Easy unsubscribe anytime.