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”
I tried the RewriteEngine off in the sub-folders htaccess, but for some reason that doesn’t seem to help – I still keep getting a 404 error…
Sounds like it could be just about anything, Mike. Without being able to investigate the situation myself, it is really difficult to determine the source of the issue. If it were me, I would try removing various bits of HTAccess from either file until something clicks. Additionally, depending on the type of script(s) that you are using for content in the subdirectory, you may want to experiment with static content, script isolation, and even directory relocation. Any of these things may help diagnose the cause of the conflict. I hope that helps!
Perishable, Is it possible to create an .htaccess file that retrieves and inbound URL it is then redirected from the root level (
htdocs
) to the subdirectory (htdocs/subdir
)?Currently an URL is typed
www.someurl.com
, and is defaulting to our main site:www.mainsite.com
. Is there a way to retrieve the "someurl.com
" and redirect it towww.mainsite.com/subdir
?Thanks in advance for any assistance you can provide!
Des
PS. Perishable, regarding post/question #63, the subdirectory is “wordpress” for our wordpress blog site that was built outside of our hosting provider (network solutions). The version of wordpress being used is 2.5.1. All of this is an FYI if you need to know.
I am hoping you can help me with my (similar) problem; it seems that you have helped many others.
My blog is about 3 weeks old. I had “Day and Name” Permalinks and all was working well. However, all of a sudden, I started getting 404s on every page, including the home page. I tried everything from theme changes to disabling plug-ins to checking htaccess files to checking mod_rewrite privileges, etc. but nothing worked. I was told that if I changed to default permalinks, it would work. I did and my site came back, but, as you can see, none of my permalinks work. Everything that I click on from my home page takes me back to the home page.
I am just about to do a clean install and I really don’t want to go back and put all my posts back in, so you could really save me here if you have an answer!
Thanks,
Doug
Hi Des, try this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?someurl\.com$
RewriteCond %{REQUEST_URI} !^/wordpress/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /wordpress/$1
Replace “someurl.com” with the actual URL. Place in root htaccess file. No other editing is required. Make sure test — the logic is sound, but I have not tested it myself..
@Doug: it looks like you beat me to it! I checked your site and the permalinks seem to be working fine. Congrats! :)
Jeff,
I’m not sure if I’m doing something wrong, but it’s not working correctly. I’m not sure if it has something to do with my wordpress files, permalinks, or something else. Gosh this is frustrating–thanks for your help, if you have any other thoughts, I’m all ears–thanks again!!! :)
Jeff,
Yes, I did beat it! Thanks for checking on me, though! I am almost embarrassed to say what I had done, but, if it helps someone else, I will…
I had, for some stupid reason, changed the URL where my blog is located by adding a /wordpress/ to it, so all my permalinks were citing /wordpress/wordpress/2008/ etc… It took me forever to notice it… oh, well!
Thanks again…
That will snag the best of ‘em, Doug. Glad to hear you knocked it out. And thanks for sharing the lesson learned — it is sure to help others in the same boat.
Cheers,
Jeff
Hi Jeff,
I have the same problem as Mike on comment 61. What I found out is by turning off the rewrite engine off, it works on the folder itself but not the individual files inside the very folder.
Meaning that if i access the directory manually e.g. my_url/abc it will list the files and then when i click on the files e.g. a photo jpg, it will work. But linking directly the jpg file e.g. my_url/abc/photo.jpg doesn’t work, it will still show the 404 page.
Any workaround on this? I been looking around and even wordpress forums doesn’t have a solution on this.
Hi Laurent, I will look into this and see if I can come up with something that works. I have an idea of why this might be happening, but need some time to test it and write it up. I will post back here once I determine the issue and (hopefully) a solution.