Latest TweetsDifference between mod_alias and mod_rewrite perishablepress.com/difference…
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 = Creative thinker. Passionate about free and open Web.
Archives
96 responses
  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. Jeff Starr

    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

    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

    @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

    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 ]