Latest TweetsOfficial Resources for #Gutenberg Block Editor: digwp.com/2018/12/resources-gu… #WordPress
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 = Web Developer. Security Specialist. WordPress Buff.
Archives
96 responses
  1. Jeff Starr

    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 November 27, 2007 @ 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 December 23, 2007 @ 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

    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 ]