New .htaccess Code for WordPress Permalinks
While manually upgrading a bunch of old WordPress sites, I realized that the WordPress htaccess rules for permalinks had changed. For many years and versions, the htaccess code that enables WordPress permalinks went unchanged, resulting in an almost sacred set of htaccess directives. Here are the original permalink rules as currently provided at the WordPress Codex:# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
When working with WordPress, that snippet is the only htaccess code that is actually required
by WordPress, and even then, it’s only necessary if you are using
so-called “pretty-permalinks” on your site. Needless to say, there are
probably millions of instances of these original permalink
rules in place on servers and sites all across the Web. The code is so
familiar that many WordPress devs can probably write it from memory. So
changing things is actually kind of a big deal.The NEW WordPress Permalink Rules
There’s really no reason to get all excited about anything. I just tend to get hyped up for anything relating to WordPress or htaccess. So mix them together and I’m all over it. For the new WordPress permalink rules, only a single line was added, and it’s a good one to have in there:RewriteRule ^index\.php$ - [L]
So that’s the new hotness, and it works great at what it does, which
we’ll get to here in a moment. For now, let’s go ahead and add that new
directive to our original htaccess ruleset to get the new WordPress permalink rules:For sites installed in 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
And for sites installed in a subdirectory:# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /subdirectory/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /subdirectory/index.php [L]
</IfModule>
# END WordPress
I’m not sure exactly when the htaccess permalink rules were
changed, and I’m not sure it really matters, but I’m thinking it
happened sometime between versions 3.0.0 and 3.0.4. I didn’t notice it
until version 3.0.4, but I am guessing that it happened at 3.0. Somebody
let me know that I’m wrong here.So what does it do?
What is the purpose of the new htaccess directive? Why change something that has worked so well for so long? Better have a good reason to go tampering, right? Yes, and as it turns out, this new rule is involved with canonicalization [1] of your WordPress URLs. I don’t think it was by any means necessary, but it’s definitely an improvement over the original code. The new directive basically strips off any danglingindex.php from your WordPress permalinks.[1] Update: As Robert points out, the new line of code is there to fix an issue with Apache’s
mod_rewrite; it has nothing to do with canonicalization, which is handled automatically by WordPress.For example, with the original permalink rules (and no other htaccess modification), the following URLs all refer to the same page:
http://your-domain.com/http://your-domain.com/index.phphttp://your-domain.com/index.php
www canonicalization, and so the new line of htaccess takes care of the other end of the URL: the index.php.
This may not seem like a big deal, but can have a big impact on script
performance, search-engine optimization, and user-experience.Updating your htaccess rules
So the question now is “do I need to go back and add this new directive to my existing permalink rules?” I think the best answer is that you don’t need to do anything – your WordPress-powered sites will continue to work just fine. But if you want the additional canonicalization that the new rule provides, then you probably should stick it in there.A couple of things to keep in mind about the new htaccess code:
- No need to update existing htaccess files unless you want to
- As with the original code, the new htaccess works in all versions of WordPress
- If you do add the new line, make sure to remove/adapt any similar/related htaccess rules
# CANONICAL WP RULES
<IfModule mod_rewrite.c>
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php [NC]
RewriteRule ^index\.php$ http://example.com/ [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
</IfModule>
This code works on any site – not just WordPress installs – to clean up both ends of the URL: the www and the index.php.
As I updated my htaccess files with the new WordPress permalink rules, I
had to remove this custom canonicalization code to make room
(functionally speaking) for the new stuff, which is a much more elegant
solution.Source: http://digwp.com/2011/01/new-htaccess-permalink-rules/







0 nhận xét:
Post a Comment