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.
GA Pro: Add Google Analytics to WordPress like a pro.

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

  1. Wow, thanks for this. I’m setting up a new blog and couldn’t figure out why the permalinks tweak wasn’t working. Turns out, it was the subdirectory path in the htaccess file was missing.

    Man, I’ve been pulling my hair out for a couple of hours now trying to figure this out and all it took was one side-by-side glance from my file to your examples to highlight the problem. I just wish I’d tumbled over this site sooner, rather than trying to weed through pages of High Geek on the WP forums. (I speak only Low Geek, I’m afraid) :-D

    You rock.

  2. Perishable 2007/09/18 9:49 am

    Thanks for the feedback, Soni — I am glad that you found the information useful. As a proud member of the “Low Geek” Society, I too found the convoluted maze of High Geek documentation far too tedious for any serious attempt at implementing something as important as WordPress permalinks. May the overlords of Low Geekdom bestow a million visitors upon your site!

  3. Fast search, fast answer, fast solution. Thank you so much !

  4. Perishable 2007/09/22 8:47 pm

    You are very welcome, Rickart!

  5. Hi,

    I have reached your website looking for a solution with my Permalinks. I have just installed my blog and when I change the setip for a more prettty permalinks, the .htaccess file is created as your example above. The problem is I cannot access my blog anymore I get the 403 Forbidden Access message. If I delete the htacces file, it works again.

    Do you have an idea what am I doing wrong.

    Thanx in advance

  6. Perishable 2007/09/30 8:35 am

    ion,

    Two questions:

    1. Which version of WordPress are you using?

    2. Is your blog installed in a subdirectory?

  7. I suspect there is something here which can resolve the problem I have been unable to resolve but I’m not sure how to use the information exactly. Everything on my site works, except for pagenav whether it be at the bottom of the main page, an archives page, etc. When you click on one of the page numbers it fails with a 404 because the url has an extra /blog in it. If that were not there it would work. I have tried varies permalink settings and still no good. I found I did not have an htaccess file so I created on and put the string of commands in it that wordpress gave me and made sure the permissions on the file were ok. I tried it in a few different subdirectors. No good. Have you any recommendations for me based on what you have learned regarding wordpress filestructure rules and the htaccess file or anything else really. I’ve been at this for months and it’s making me crazy.
    Thanks in advance for any advice.

  8. Perishable 2007/10/11 7:37 pm

    Hi kym,
    I suspect this has something to do with the plugin used to generate the page links. May I ask which plugin you happen to be using?
    Regards,
    Jeff

  9. Oh I’m sorry – I should have said in my original bit of information that this problem occurs no matter what plugin (because I have tried a few hoping it fixes the problem) I use or if I use none at all. I just deactivated the plugin so you can see what I mean. When the page is assembled that /blog is already in there. I should also have said that the wp install is in a sub directory and not htdocs. I’ve thought that to be the root cause of the issue but assumed there was some way to work around it which is why I thought, when I read this thread, that perhaps something about the htaccess file and wp rules would make a difference.

  10. Perishable 2007/10/15 8:16 am

    kym,
    I would recommend scouring your blog for key instances of the word “blog”. For any WordPress-powered site, there are two fundamental components: the database and the physical files (.php, etc.). Assuming that you have closely inspected your htaccess file for any extra instances of the term “blog”, the next thing to do would be scour the database. Using phpMyAdmin (or whatever), log into your WordPress database and execute a search for “blog” on all tables. Depending on the size of your database, you will get the phone book in return, but with a few logical eliminations, you should be able to narrow the search down to one or two target tables. For example, it probably is not necessary to investigate any occurrences of “blog” that are found in anti-spam plugins, feed-reader plugins, and other peripheral plugins; on the other hand, you will definitely want to examine any hits from your options table and any other core WordPress tables containing the term. Likewise, you should also do a comprehensive search of your site files for the target term, and check out each and every result. The point is, that the extra “blog” that is being inserted into the navigation routine has to be coming from somewhere (duh), and so it is just a matter of tracking it down. It may have been 100 times easier to locate a word that is not nearly as common as “blog”, but we work with what we have been given ;) Anyway, I hope this has helped in some way. I realize I am making all sorts of assumptions here, but I am only trying to help ;) Good luck!

  11. I have scoured the files that are available via the admin, edit themes interface but not gone into the server and looked at everything there. I didn’t think about that. I will try it. I should also note that I didnt have an htaccess file. I created one because I’d hoped creating one with the right stuff would fix my problem. Maybe I’ll look into that a little deeper.

    I greatly appreciate your help. Thank you for taking the time! If I sort it out I’ll let you know. Thanks again.

  12. jeff, i’ve seen a lot of articles but yours is one of the best resources on the topic.

    i found it because i am moving from MT to WP. i have mt using this permalink format

    /mt/archives/postname.html

    using wp-admin, i set the custom permalink in WP to

    /mt/archives/%postname%.html

    i will use htaccess to convert dashes to underscores that are used in lieu of spaces in the post names.

    keeping in mind the fact that i am a WP newb, it seems to be the simplest way to guarantee that my google SERPS won’t get dropped… i don’t even know why i would do it another way.

    my question is: do you see a problem with doing it this way?

    thanks

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 »
BBQ Pro: The fastest firewall to protect your WordPress.
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.