The htaccess Rules for all WordPress Permalinks
Published Wednesday, June 14, 2006 @ 6:28 pm • 89 Responses
Note: this article specifically applies to WordPress version 2+ running on Apache servers.
We recently performed a series of tests on a fresh installation of WordPress 2.0.2 to determine the exact htaccess rewrite rules that WordPress writes to its htaccess file for various permalink configurations. Under the WP admin option menu, WordPress lists four choices for permalink structure:
- Default: http://perishablepress.com/press/?=123
- Date and name based: http://perishablepress.com/press/index.php/2006/06/14/sample-post/
- Numeric: http://perishablepress.com/press/index.php/archives/123
- Custom: /%year%/%monthnum%/%day%/%postname%/
The "default" option is to not use permalinks. The "date and name based" setting invokes the /index.php/%year%/%monthnum%/%day%/%postname%/ rule pattern. The "numeric" setting invokes the /index.php/archives/%post_id% rule pattern. And the "custom" setting invokes whatever pattern is specifically chosen. For our "custom" option, we chose the /%category%/%author%/%postname%/ pattern.
For the test, we began with the common "date and name based" permalink configuration. Then, after invoking the chosen permalink structure, the htaccess file was downloaded and the new WP rules were recorded. After this, the admin permalink settings were switched back to the "default" configuration. Finally, we deleted the new rules and uploaded the htaccess file. This process was diligently repeated for each different permalink configuration. It should also be noted that blog performance was checked during each round of testing.
The results indicate conclusively that WordPress uses the exact same set of htaccess rules for all permalink configurations. Surely this information is available elsewhere on the internet, however we were experiencing several inconsistencies related to permalink structure that inspired us to determine for ourselves the precise htaccess rules for WordPress permalinks.
Without further ado, the htaccess rules for all WordPress permalinks1 are precisely either #1 or #2:
[ #1 ] If WordPress installed in the root directory »
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
[ #2 ] If WordPress installed in a subdirectory called "foo" »
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /foo/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /foo/index.php [L]
</IfModule>
# END WordPress
1 Note: 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.
About this article
Related articles
- htaccess Combo Pack: WordPress Permalinks and non-www Redirect
- Comprehensive URL Canonicalization via htaccess for WordPress-Powered Sites
- WordPress Feedburner HTAccess Redirect for Default (Non-Permalink) Feed URLs
- Permalink Evolution: Customize and Optimize Your Dated WordPress Permalinks
- Permalink Enlightenment
- Redirect WordPress Feeds to Feedburner via htaccess (Redux)
- WordPress Lessons Learned, Part 1: Permalink Structure
Dialogue
89 Responses Jump to comment form
January 16, 2007 at 2:23 am
Wow, i never knew all permalinks structure had the same htaccess rules. Thx 4 the info!
May 17, 2007 at 11:07 am
Yet another thank you for putting this together!!! Chalk me up as another happy user of your knowledge-share.
June 9, 2007 at 10:15 pm
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.
August 8, 2007 at 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!
September 4, 2007 at 7:38 pm
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.
September 6, 2007 at 2:13 am
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.
September 17, 2007 at 4:26 pm
Wow, thanks for this. I’m setting up a new blog and couldn’t figure out why the permalinks tweak wasn’t working. Turns out, it was the subdirectory path in the htaccess file was missing.
Man, I’ve been pulling my hair out for a couple of hours now trying to figure this out and all it took was one side-by-side glance from my file to your examples to highlight the problem. I just wish I’d tumbled over this site sooner, rather than trying to weed through pages of High Geek on the WP forums. (I speak only Low Geek, I’m afraid) :-D
You rock.
September 20, 2007 at 3:06 am
Fast search, fast answer, fast solution. Thank you so much !
September 29, 2007 at 10:38 am
Hi,
I have reached your website looking for a solution with my Permalinks. I have just installed my blog and when I change the setip for a more prettty permalinks, the .htaccess file is created as your example above. The problem is I cannot access my blog anymore I get the 403 Forbidden Access message. If I delete the htacces file, it works again.
Do you have an idea what am I doing wrong.
Thanx in advance
October 11, 2007 at 10:43 am
I suspect there is something here which can resolve the problem I have been unable to resolve but I’m not sure how to use the information exactly. Everything on my site works, except for pagenav whether it be at the bottom of the main page, an archives page, etc. When you click on one of the page numbers it fails with a 404 because the url has an extra /blog in it. If that were not there it would work. I have tried varies permalink settings and still no good. I found I did not have an htaccess file so I created on and put the string of commands in it that wordpress gave me and made sure the permissions on the file were ok. I tried it in a few different subdirectors. No good. Have you any recommendations for me based on what you have learned regarding wordpress filestructure rules and the htaccess file or anything else really. I’ve been at this for months and it’s making me crazy.
Thanks in advance for any advice.
October 12, 2007 at 6:21 am
Oh I’m sorry - I should have said in my original bit of information that this problem occurs no matter what plugin (because I have tried a few hoping it fixes the problem) I use or if I use none at all. I just deactivated the plugin so you can see what I mean. When the page is assembled that /blog is already in there. I should also have said that the wp install is in a sub directory and not htdocs. I’ve thought that to be the root cause of the issue but assumed there was some way to work around it which is why I thought, when I read this thread, that perhaps something about the htaccess file and wp rules would make a difference.
October 15, 2007 at 7:52 pm
I have scoured the files that are available via the admin, edit themes interface but not gone into the server and looked at everything there. I didn’t think about that. I will try it. I should also note that I didnt have an htaccess file. I created one because I’d hoped creating one with the right stuff would fix my problem. Maybe I’ll look into that a little deeper.
I greatly appreciate your help. Thank you for taking the time! If I sort it out I’ll let you know. Thanks again.
October 16, 2007 at 5:55 pm
jeff, i’ve seen a lot of articles but yours is one of the best resources on the topic.
i found it because i am moving from MT to WP. i have mt using this permalink format
/mt/archives/postname.html
using wp-admin, i set the custom permalink in WP to
/mt/archives/%postname%.html
i will use htaccess to convert dashes to underscores that are used in lieu of spaces in the post names.
keeping in mind the fact that i am a WP newb, it seems to be the simplest way to guarantee that my google SERPS won’t get dropped… i don’t even know why i would do it another way.
my question is: do you see a problem with doing it this way?
thanks
November 12, 2007 at 10:06 pm
Thank you for your insight into word press 404 error pages and the permalink structure! I was lost but now I am back up functioning perfectly.
November 27, 2007 at 3:22 pm
You are the absolute best! This is the fix of all fixes! I owe you a million!
December 23, 2007 at 3:39 pm
Hi James,
Boy am I glad I found your blog. :) James, you are my hope…
I tried it with your great instructions above, to no avail though. I always get the same error message; that is,
“Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.”
This happens whenever I change the permalink settings. Once I click on that “Update Permalink Structure” button the browser displays the above message.
That means, I’m then unable to access anything, the site is unaccessible, until I delete the .htaccess file from the root folder (where the blog is located.)
In other words, I installed WordPress 2.3.1 and it seems I cannot modify the permalink structure.
Do you think it can be a problem with my Web host? Although they clearly state I can use a WordPress blog on my site and even offer a one-click installation. Or perhaps I’ll have to indicate a subdirectory that exists on the server for my specific domain, somehow? (like ‘public_html’, which didn’t work either though)
Any thoughts?
My best,
Marcus
P.S. Marry Christmas! :)
January 2, 2008 at 11:01 am
Thanks so much for this post, it helped me greatly in understanding how wordpress implements permalinks under the hood. I installed wordpress under the root directory of my site and realized with mod_rewrite that any other sub-directories off of root that I previously had used would not work…and instead be routed through wordpress and eventually the error page. Is there any way to add exclusions for subdirectories? Thanks for any help.
January 2, 2008 at 2:53 pm
No, only one installation of wordpress, I just installed it in the root www folder with my webhost. I had other subfolders outside of wordpress I was using for various things. With the rewrite rules on, I can’t access those subfolders anymore, since wordpress picks it up as an error in what it believes to be a permalink. So I was wondering if there was a way to exclude certian folder names from the rewrite rules. Thanks again, I haven’t found any info on wordpress installations with these requirements so I really appreciate your help.
January 2, 2008 at 5:40 pm
Thank you! That worked! I was concerned doing this would mess up my sub-domain setup because for some reason the sub-domain info looked like it was in the rewrite rules of the subfolder in question. But I removed the rewrite rules from the subfolder, and my sub-domain still worked, this time without getting the wordpress error!
January 8, 2008 at 6:53 pm
Thank you, Thank you! I spent hours last night trying to figure this out and stumbled upon this today!
January 17, 2008 at 11:01 pm
Hi Perishable,
I’m having a similar issue with wordpress and .htaccess.
I have multiple subdomains under one server. Within each subdomain is a WordPress blog. To avoid canonicalization, I rewrite domain.com to www.domain.com. Unfortunately, wordpress only works as a folder extension of highlevel domain. (eg. maindomain.com/subdomain/blog/)
I tried a few tricks mentioned here and on WP.org, but to no avail.
I was hoping with your experience you might have a quick solution.
Thanks in advance,
Scott
February 20, 2008 at 5:24 pm
my site was hacked last night and as part of my research to restore it, two articles of yours came up. I noticed this because of your theme design. Thanks for posting the information. It’s been very helpful.
March 22, 2008 at 6:24 am
Thank you so much for putting this up. I read it and the comments others left but I think my situation is a bit different and I was hoping you’d be able to help me out.
My blog is located at http://domain.ca/blog. I have a .htaccess file in that folder with chmod 666
I put your code from #2 above into the .htaccess file, changing the two instances of ‘foo’ to ‘blog’ (am I supposed to remove the #?)
I activated date and name-based permalinkss then I went to my site and click on the title of an entry, with the hope that it would show only that entry on the page and the new improved permalink above. Instead, my header image disappeared and all the entries on the page are still visible.
However, when I use the default permalinks and click on the title of the entry, the single entry shows up and I can still see my header image.
I hope this makes sense. Would you have any idea why this is acting this way?
Thank you for your time!
March 25, 2008 at 7:48 pm
Thank you very much! I think the whole process of submitting my problem to you and thinking on what you said helped me figure out my problem.
My header image wasn’t showing up with individual posts because I had a relative link instead of the absolute link, so it wasn’t related to the above code not working.
I appreciate your help. :)
April 7, 2008 at 6:54 pm
So I’m curious if it is then possible to password-protect just one Wordpress url. (Wordpress has a post-level protection built-in but it stinks.) Would you not write this beneath your rewrite rules?
# password-protect single file
<Files index.php?p=67>
AuthType Basic
AuthName "This area is restricted to registered users"
AuthUserFile /icslte/.htpasswd
Require valid-user
</Files>
May 13, 2008 at 1:45 pm
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?
May 17, 2008 at 4:05 am
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?)
June 15, 2008 at 1:31 pm
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/
June 19, 2008 at 6:49 am
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.
June 19, 2008 at 10:00 am
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. :)
June 24, 2008 at 12:56 am
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?
June 25, 2008 at 12:45 am
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…
August 14, 2008 at 11:19 am
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 to www.mainsite.com/subdir?
Thanks in advance for any assistance you can provide!
Des
August 14, 2008 at 11:23 am
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.
August 15, 2008 at 11:52 am
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
August 17, 2008 at 10:50 am
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!!! :)
August 17, 2008 at 5:48 pm
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…
September 16, 2008 at 11:26 pm
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.
September 17, 2008 at 8:57 pm
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.
September 22, 2008 at 7:59 pm
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
September 24, 2008 at 8:00 pm
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.
October 2, 2008 at 9:45 am
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
October 6, 2008 at 6:51 am
Those .htaccess rules do not fix non-www and www duplicate content issues.
That is a vital thing to add to the mix.
October 9, 2008 at 8:07 am
Holy crap, you just saved my A$$! Thank you!
Trackbacks / Pingbacks
- robinadr › Greedy WordPress Rewrite Rules
- AppliedI.net Windows Webhosting » Blog Archive » The better way to do permalinks in Wordpress on Windows
- Enabling and setting up permalinks (custom) in Wordpress | Rotheblog
- K4mpun6 Curh4t » Blog Archive » The htaccess Rules for all WordPress Permalinks
- Team Programming Dragon.編程龍 » Blog Archive » Permanlink 美化
- Wordpress CMS Part 1: Settings And Plugins | Custom Wordpress Theme Design
- Про перенесення WP-блоґу на інший хостинг, utf-8 та latin1
- WPVote Needs To Improve Published News Section | More Than Scratch The Surface
Share your thoughts..
← Previous post • Next post →
« Get Back • Geek Code Nonsense »
1 • Mac
August 9, 2006 at 9:36 pm
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.