htaccess Combo Pack: WordPress Permalinks and non-www Redirect
WordPress users employing permalinks via htaccess to optimize their dynamic URLs transform complicated-looking links such as:
http://example.com/blog/index.php?page=33
..into search-engine friendly links such as:
http://example.com/blog/post-title/
Every rewritten URL relies on a common set of htaccess rules to transform the links. The htaccess rules for all WordPress permalinks look like this for root WP installations:
# BEGIN WordPress
<ifmodule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</ifmodule>
# END WordPress
..and like this for subdirectory WP installations:
# BEGIN WordPress
<ifmodule mod_rewrite.c>
RewriteEngine On
RewriteBase /subdirectory/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /subdirectory/index.php [L]
</ifmodule>
# END WordPress
Unfortunately, although this default htaccess code affects all WordPress-generated URLs, it does not take into consideration the basic canonicalization issue of the infamous www prefix.
As visitors navigate through your WordPress-powered site, each and every page is accessible via at least two (and probably several more) distinctly different URL addresses: one with the www prefix and one without.
This of course presents duplicate content issues that tend to lower the ranking effectiveness of your pages. Fortunately, it is relatively easy to eliminate duplicate content generated via identical www and non-www content.
Given the previously defined, default WordPress htaccess rules for permalinks, simply insert the following code after the line RewriteEngine On:
RewriteCond %{HTTP_HOST} ^www\.domain\.tld$ [NC]
RewriteRule ^(.*)$ http://domain.tld/$1 [R=301,L]
So, to clarify, let’s say your WordPress installation is located in the root html directory of your domain. In this case, replace the default permalink htaccess code with this:
[ WordPress installed in site root ]
# BEGIN WordPress
<ifmodule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.domain\.tld$ [NC]
RewriteRule ^(.*)$ http://domain.tld/$1 [R=301,L]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</ifmodule>
# END WordPress
Now, all visitors will be directed to the non-www version of the permalink URL for your pages. Similarly, when your blog is installed in a subdirectory of the html root, replace the default code with this:
[ WordPress installed in subdirectory ]
# BEGIN WordPress
<ifmodule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.domain\.tld$ [NC]
RewriteRule ^(.*)$ http://domain.tld/subdirectory/$1 [R=301,L]
RewriteBase /subdirectory/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /subdirectory/index.php [L]
</ifmodule>
# END WordPress
This code will ensure that all pages within the subdirectory are served with non-www permalinks. To rewrite all pages above the subdirectory level, such as pages located “above” the blog in the root html directory, add a copy of the following code to your site’s root htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.domain\.tld$ [NC]
RewriteRule ^(.*)$ http://domain.tld/$1 [R=301,L]
One final note: if you would rather employ the www-versions of your site’s pages as the official canonicalized URLs, replace these two lines:
RewriteCond %{HTTP_HOST} ^www\.domain\.tld$ [NC]
RewriteRule ^(.*)$ http://domain.tld/$1 [R=301,L]
..with these:
RewriteCond %{HTTP_HOST} ^domain\.tld$ [NC]
RewriteRule ^(.*)$ http://www.domain.tld/$1 [R=301,L]
And that should do it. With our new and improved WordPress permalink rules, we eliminate duplicate content, serve search-engine friendly permalinks, and thus preserve more link equity and SEO value for our pages. Nice! ;)
Related articles
- The htaccess Rules for all WordPress Permalinks
- Comprehensive URL Canonicalization via htaccess for WordPress-Powered Sites
- Redirect WordPress Feeds to Feedburner via htaccess (Redux)
- Permanently Redirect a Specific IP Request for a Single Page via htaccess
- Redirect All Requests for a Nonexistent File to the Actual File
- Universal www-Canonicalization via htaccess
- Permalink Evolution: Customize and Optimize Your Dated WordPress Permalinks
About this article
This is article #413, posted by Jeff Starr on Monday, October 01, 2007 @ 11:08am. Categorized as Function, WordPress, and tagged with apache, code, htaccess, mod-rewrite, permalink, redirect, tips, tricks, WordPress. Updated on November 05, 2007. Visited 25095 times. 10 Responses »
Bookmark • Trackback • Comment • Subscribe • Explore
« 88Teeth Site Redesign • Up • Fixing Mint after Switching Servers »
1 • October 1, 2007 at 12:11 pm — Louis says:
I have a good and a bad new for you.
The bad new is that this method obsolete.
The good new is that this method has been made absolete by the new WordPress 2.3, which takes care of the canonical URL automatically.
Indeed, your work is still useful and pedagogic, but it’s important to know that the last WP does it internally.
This said, i want to thank you for the nofollow serie you wrote. It must’ve taken a lot of time, so thanks for sharing this with us !