Latest TweetsAll o' me plugins freshly updated and ready for WP 5.0 :) profiles.wordpress.org/special…
Perishable Press

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! ;)

Jeff Starr
About the Author Jeff Starr = Fullstack Developer. Book Author. Teacher. Human Being.
Archives
34 responses
  1. Jeff Starr

    @Bill Hazelton: Sure. Place the rewrite directives in the subdirectory’s HTAccess file. Then add the following to the root HTAccess:

    RewriteCond %{HTTP_HOST} ^domain\.tld$ [NC]
    RewriteRule ^(.*)$ http://www.domain.tld/$1 [R=301,L]

    To account for the root case. Edit as necessary.

  2. At first I didn’t use permalinks at my site. Also, I used and I’m still using a non-www to www redirection in my .htaccess:

    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
    RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

    The problem came when I started using permalinks. The non-www redirection stopped working and a page titled “301 Moved Permanently” showed with this error:

    Moved Permanently
    The document has moved here.

    Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.

    So it seems that the code for the permalinks added to the .htaccess is overriding the non-www redirection, or there is some kind of trouble between the codes. The code for the permalink structure is this:

    # BEGIN WordPress

    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]

    # END WordPress

    Could you help me? Thanks in advance.

  3. Jeff Starr

    Hi Inv, sounds like you might have introduced a syntax error in your htaccess code. You may also want to try switching the order in which the two sets of directives appear in the file (i.e., processing order).

  4. Hi Jeff,

    Can you help me with this slight issue I’m experiencing. I’ve got WP installed in a subdirectory, and I want the non-www version to point to the www version. I followed your instructions and it works fine except for one thing:

    http://mydomain.com/blog does not redirect properly; however http://mydomain.com/blog/ does (notice the trailing slash).

    Can you advise on how to fix this?

    Regards.

  5. Jeff Starr

    Not sure what the issue might be.. a lot depends on other factors, such as what scripts you are using, the version of WordPress, and the other contents of your htaccess file(s).

    I suggest that you look into the information provided in my article on comprehensive WordPress canonicalization. It takes into account just about every possible canonicalization issue that you might encounter while using WordPress.

    Also, keep in mind that newer versions of WordPress (from 2.5 forward, if I recall) handle the www/non-www redirect automatically, depending on how you specify your blog’s URL in the Admin Settings area. If you include the “www” prefix there, all of your pages should be redirected properly.

  6. Fantastic solutions here! Thank you for the insight! I located a solution to having WordPress not override my custom rewrite, however it only appears to work with the Permalinks setting set to Default. I enable permalinks and overrite the WordPress settings with my own, however still receive a WordPress Page Not Found. Is there any way to tweak the below so it will work with WordPress Permalinks as well?:

    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !^/(podcast|podcast/feed) [NC]
    RewriteRule . /index.php [L]
    RewriteRule ^podcast/?$ /index.php?category_name=podcast [QSA,L]
    RewriteRule ^podcast/feed/(atom|rss2)/?$ /index.php?feed=$1&amp;category_name=podcast [QSA,L]

  7. Jeff Starr

    Hi Brett, one thing that you might try is disabling the rewrite rules in the target directories. Here are three different ways of doing it.

  8. Jeff, i have been change my permalinks, & i also change my htaccess format! when i open my single post blog links, its work! But my category spoilers links was broken (error 404)! can you help me!

    Thanks for your attention!

  9. Jeff Starr

    @Rudy: Are you using any plugin for redirection (in addition to htaccess)?

  10. Jessica Bosari March 25, 2010 @ 2:12 am

    You seem like you really have got it together here. After countless searches, I humble beg your assistance!

    I want to keep my permalinks at default because I use the page id as a product id on my eShop WordPress installation. The problem is that the 404 redirect does not function. My htaccess file is where it should be and writeable, but when I change permalinks back to the default, 404 does not redirect. It just stays on the same page. When I use the custom setting, 404 works exactly as it should.

    Any help you could offer would be a God-send.
    here’s the site:
    http://www.rewindyarn.com

  11. Jessica Bosari March 25, 2010 @ 2:15 am

    Duh!!! I hate wasting my time this way…and yours. Finally figured out that just setting to page id on my permalinks works. But still…why doesn’t the 404 redirect function with default permalink settings?

  12. I have been solve my trouble shoot permalinks by using 2 plugins! Advance Permalinks & Category permalinks! You can ceck it out in wordpress.org Extention!

    But anyway, i use 2 different type permalinks for my single post & Category!

    %category/post% for single post & category for category permalinks (without %)! & its work! ^_^

[ Comments are closed for this post ]