Latest TweetsWordPress and the Blank Target Vulnerability (aka rel noopener + noreferrer): perishablepress.com/wordpress-… #WordPress #security #html
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. Book Author. Secretly Important.
Archives
96 responses
  1. Thank you. You have no idea how helpful this was. A site I recently became responsible for (not my own) was hosed by wp’s htaccess shenanigans. Thanks posting.

  2. Jeff Starr

    Happy to help — thanks for the feedback!

  3. Wow, i never knew all permalinks structure had the same htaccess rules. Thx 4 the info!

  4. Yet another thank you for putting this together!!! Chalk me up as another happy user of your knowledge-share.

  5. Hi,

    Thanks for the tutorial. My .htaccess file follows the form of #1 above, but I am unsure how to setup permalinks to pages in wordpress, not posts. Is there a specific way of doing this? Much appreciated.

  6. Peter Dowse August 8, 2007 @ 8:25 pm

    Hi

    Thank you so much for publishing this article. This helped me to upload the correct htaccess file to my server and get kick arse SEO friendly permalinks working.

    Thank you so much….I have been looking for days on the net and this is the only site that has answered my questions AND shown me how to get the proper code in my htaccess file.

    Cheers guys….thanks very much!

  7. Jeff Starr

    You are very welcome! Thanks for the feedback!

  8. I’m not much of a coder and also am relatively new to WordPress and blogging.

    When setting up a custom permalink last night WP wrote to my .htaccess – both in my root AND blog folder – as in example #2 above.

    My blog is installed off the html root in a folder called – blog.

    The WP entry in the root .htaccess prevented me from publishing my website. The entry in the /blog sub-folder I’m wondering if that should be as in example #1 above or, just leave it as is as in example #2?

    When I totally removed the WP permalinks entry from the root .htaccess then I could connect and publish to my website again ok.

    If my blog is in a subfolder called blog with its own .htaccess then should the WP entry have the subfolder name /blog in there or not?

    Thank you.

  9. Jeff Starr

    Michael,

    When WordPress is installed in a subdirectory, the root htaccess file does not need any WP-related rewrite rules.

    The subdirectory htaccess file does need rewrite rules if permalinks are to be used. In this case, the name of the subdirectory must be included in the rewrite rules.

    Thus, adapt the code from case #2 and place it into the htaccess file located in the subdirectory containing your blog. Nothing else should be needed..

    Regards,
    Jeff

  10. Thanks a lot for your help and quick response Perishable. I appreciate that. I’m beginning to feel like an htaccess expert now :)
    Cheers. and keep up the good info. in the blog. We all appreciate that.

  11. Jeff Starr

    My pleasure, Michael — glad to be of assistance ;)
    Cheers!

[ Comments are closed for this post ]