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”
Hi Russell,
It has been awhile since I have dealt with this issue, but if I remember correctly, the trick with WordPress URLs involves using permalinks to get around Apache’s miserable support for query strings. Once you have permalinks enabled, the htaccess code given in your comment should work just fine. I think..
Thanks for the good info. I have a problem though. I used version #1 above and was able to generate a link to page from a blog post with the right URL structure, but the content is not being pulled. Can you take a look and tell me what I’m doing incorrectly?
Hi Scott,
Without proper access, it is difficult to diagnose for certain the cause of the issue. Based on the URLs having been rewritten, it appears that the htaccess code is working correctly. This leads me to question whether the permalink structure has been defined in the WordPress Admin area. Unless you specify the desired format via WordPress, the htaccess rules will direct the links to nowhere. I hope that helps — check it out and let me know how it goes..
This info saved me. It is the only page I found that gives the correct .htaccess code AND indicates that the name of the WP subdirectory (if you have one) MUST be in the .htaccess file EVEN if the file in in that subdirectory. Finally I have an rss feed.
However, is is /?feed=atom. How can I change it to feed=rss? (Do I really needto change it?)
Thanks for the positive feedback, Ben. It is great to know that this article continues to help people with their WordPress permalink configuration.
As for the location/URL of your WordPress-generated feed(s), perhaps this article will shed some light on the issue :)
I was setting up a word-press comic for a friend and it didn’t set the .htaccess for me, as my host is paranoid about permissions… Thanks for the invaluable gem here :)
The site, if you’re interested, is here:
http://www.blue-world.co.uk/jaydenandcrusader/
My pleasure, Kirrus — thanks for the positive feedback! Best of luck to your friend’s site ;)
So I am trying to setup WP 2.5.1 with Permalinks. I have done this before with 3 different blogs so I figured I know what I am doing. Of course, they are on a different host so…
Anyway, I set AllowOverride All in the httpd.conf file, then setup the .htaccess correctly (checked it 3 times), setup permalinks, and made a new post. Didn’t work.
On the server we use Apache and a *nix OS (not sure which one since I didn’t set it up).
Any insights would be appreciated. And thanks for the wonderful article that made it all clear.
Never mind, I dug a little deeper and realized that whoever setup the httpd.conf file left the default directory entry in there and did not put the actual one in. Once I did that and restarted Apache it worked. :)
Glad to hear you got it working, Rich! :)
Hi,
All of the above information is great, but I have a slightly different problem:
On of my sites has a WordPress blog in the root of the domain, and another website/script in a sub-folder that works with an index.php file.
As a result the pages in the sub-folder are displayed as a WordPress 404 page.
I have tried turning the Rewriteengine off in the sub-folders htaccess, but that doesn’t seem to help as I guess the WordPress rewrite rule precede and override it in the folder structure.
Is there a way to modify the WordPress htaccess so that it ignores certain folders?
Hi Mike, have you seen this article on allowing hotlinking in specific directories? That article provides some excellent techniques that should apply to the WordPress
Rewrite
directives. By simply adding this to your subfolder’s HTAccess file:# disable hotlink protection
RewriteEngine off
..the WordPress permalink rules should be disabled for all content within that subdirectory. Good luck!