The .htaccess Rules for all WordPress Permalinks
</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.
The 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.
96 responses to “The .htaccess Rules for all WordPress Permalinks”
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.
Happy to help — thanks for the feedback!
Wow, i never knew all permalinks structure had the same htaccess rules. Thx 4 the info!
My pleasure!
Yet another thank you for putting this together!!! Chalk me up as another happy user of your knowledge-share.
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.
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!
You are very welcome! Thanks for the feedback!
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.
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
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.
My pleasure, Michael — glad to be of assistance ;)
Cheers!