Optimize Your Dated Permalinks


How to streamline and maximize the effectiveness of your WordPress URLs by using htaccess to remove extraneous post-date information: years, months, and days.

/%year%/%monthnum%/%day%/%postname%/

The benefits of using this format are primarily organizational in nature. Post-date information that is “built-in” to every URLprovides immediate, “at-a-glance” knowledge of post “freshness”. Looking ahead ten, twenty or even a hundred years into the future of the blogosphere, there will be trillions of posts and articles, each with their own unique URL. Archived copies of content may or may not include creation date: dynamically archived pages require deliberate database queries, while those archived statically may no longer have access to post-date data. Including post dates in permalinks provides permanent, facilitative record of content origination. Needless to say, most adopters of dated permalinks probably jump on board because the WordPress Admin makes it super-easy to follow the crowd.

Over time, however, as understanding of search engine optimization permeated the blogosphere, many people who had embraced such “dated URLs” began rethinking their approach to permalinks. Eventually, the trend had reversed, as SEO-savvy bloggers avoided dated permalinks like spider pig. These days, a majority of bloggers initialize their permalinks with either a “category/name” or even a name-only URL

format:

/%category%/%postname%/ (or) /%postname%/

Of course, the benefits to this simplified structure are largely utilitarian in nature. Removal of post-date information effectively reduces the length of permalinks. Shorter permalinks provide greater usability for both humans and machines: people may find such URLeasier to read, while search engines may interpret the permalink as containing a more concentrated array of keywords. Further, search engines such as Google often display a limited number of characters in their search results. Elimination of expendable characters from your URLs results in more (if not all) of your actual post title being displayed to people as they scan the search results. Finally, shorter permalinks are simply easier to work with. They are easier to share, require (slightly) less bandwidth, and look considerably cleaner.

So, as we make our way into 2008, it appears that it is time to evolve our permalinks toward cleaner, shorter, more concise formats, with redundant information such as “year/month/day” either entirely omitted or dutifully removed. If you are setting up a new WordPress-powered site, and have not yet decided on a permalink structure, I would highly advise against inclusion of date information. Likewise, if you are running an established site that has been using dated permalinks for any length of time, you may want to join fellow bloggers such as Rick Beckman and remove the dates from your URLs.

How to remove the “year/month/date” portion of dated permalinks

Although there are free WordPress plugins available for changing your permalinks, we prefer to handle URL redirection withApache/htaccess rather than PHP because it requires fewer system resources and is executed with greater speed. One final note before we begin: the purpose of this tutorial involves removing date information from all future permalinks and redirecting allpreexisting permalinks to their restructured counterparts. Thus, if you are setting up permalinks for a new blog (or one with only a few posts), the second part of this tutorial may not be required — a simple change of permalink structure via the WP Admin (as explained below) may be all that is needed. That said, let’s begin..

Part 1: Update your WordPress Options

The first step in creating “post-name-only” permalinks is to update your WordPress permalink structure in the Permalinks Options page of the WordPress Admin. Using the Custom structure option, customize your permalink structure as follows:

/%postname%/

After entering the post-name-only permalink structure, save the changes and test your pages. Remember to check different types of views — home, single, archive, page, search, etc. — to ensure that your new permalinks are working as expected. Once this is done, all future posts will feature the dateless permalink structure. In the second part of our tutorial, we will redirect all requests for old versions of your URLs to their newly configured counterparts.

Part 2: Update your htaccess file

The second step in creating “post-name-only” permalinks involves modifying your root or subdirectory htaccess file to ensure that old permalinks are redirected to, and served as, your new permalinks. Examine each of the scenarios described below, determine which method applies to your specific setup, and implement the required steps.

Option 1: Remove “year/month/day” from permalinks with WordPress installed in the ROOT directory

This method removes the “year/month/day” portion of permalinks for blogs located within the domain’s root directory. So, for example, if your old permalinks looked like this:

http://domain.tld/2008/08/08/post-title/

..then the htaccess code provided in this section will transform them into this:

http://domain.tld/post-title/

Locate your blog’s permalink htaccess rules. Then, place the following code directly after the line containing the RewriteBasedirective:

# remove all permalink date info for blog in root directory RewriteRule ^([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/([^/]+)/?$ http://domain.tld/$4/ [R=301,L]

Remember to edit the “domain.tld” to match that of your own. No other changes are necessary. Test like crazy. After verifying that everything works as intended, sit back and enjoy your new optimized permalinks!

Option 2: Remove “year/month/day” from permalinks with WordPress installed in SUBDIRECTORY

This method removes the “year/month/day” portion of permalinks for blogs located within a subdirectory. So, for example, if your old permalinks looked like this:

http://domain.tld/subdirectory/2008/08/08/post-title/

..then the htaccess code provided in this section will transform them into this:

http://domain.tld/subdirectory/post-title/

Locate your blog’s permalink htaccess rules. Then, place the following code directly after the line containing the RewriteBasedirective:

# remove all permalink date info for blog in subdirectory RewriteRule ^([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/([^/]+)/?$ http://domain.tld/subdirectory/$4/ [R=301,L]

Remember to edit the “domain.tld/subdirectory” to match that of your own. No other changes are necessary. Test like crazy. After verifying that everything works as intended, sit back and enjoy your new optimized permalinks!

Option 3: Remove “year/month” from permalinks with WordPress installed in ROOT directory

This method removes the “year/month” portion of permalinks for blogs located within the domain’s root directory. So, for example, if your old permalinks looked like this:

http://domain.tld/2008/08/post-title/

..then the htaccess code provided in this section will transform them into this:

http://domain.tld/post-title/

Locate your blog’s permalink htaccess rules. Then, place the following code directly after the line containing the RewriteBasedirective:

# remove year and month info from permalinks for blog in root directory RewriteRule ^([0-9]{4})/([0-9]{1,2})/([^/]+)/?$ http://domain.tld/$3/ [R=301,L]

Remember to edit the “domain.tld” to match that of your own. No other changes are necessary. Test like crazy. After verifying that everything works as intended, sit back and enjoy your new optimized permalinks!

Option 4: Remove “year/month” from permalinks with WordPress installed in SUBDIRECTORY

This method removes the “year/month/day” portion of permalinks for blogs located within a subdirectory. So, for example, if your old permalinks looked like this:

http://domain.tld/subdirectory/2008/08/post-title/

..then the htaccess code provided in this section will transform them into this:

http://domain.tld/subdirectory/post-title/

Locate your blog’s permalink htaccess rules. Then, place the following code directly after the line containing the RewriteBasedirective:

# remove year and month info from permalinks for blog in subdirectory RewriteRule ^([0-9]{4})/([0-9]{1,2})/([^/]+)/?$ http://domain.tld/subdirectory/$3/ [R=301,L]

Remember to edit the “domain.tld/subdirectory” to match that of your own. No other changes are necessary. Test like crazy. After verifying that everything works as intended, sit back and enjoy your new optimized permalinks!

Option 5: Remove the “year” from permalinks with WordPress installed in ROOT directory

This method removes the “year” portion of permalinks for blogs located within the domain’s root directory. So, for example, if your old permalinks looked like this:

http://domain.tld/2008/post-title/

..then the htaccess code provided in this section will transform them into this:

http://domain.tld/post-title/

Locate your blog’s permalink htaccess rules. Then, place the following code directly after the line containing the RewriteBasedirective:

# remove year info from permalinks for blog in root directory RewriteRule ^([0-9]{4})/([^/]+)/?$ http://domain.tld/$2/ [R=301,L]

Remember to edit the “domain.tld” to match that of your own. No other changes are necessary. Test like crazy. After verifying that everything works as intended, sit back and enjoy your new optimized permalinks!

Option 6: Remove “year” from permalinks with WordPress installed in SUBDIRECTORY

This method removes the “year” portion of permalinks for blogs located within a subdirectory. So, for example, if your old permalinks looked like this:

http://domain.tld/subdirectory/2008/post-title/

..then the htaccess code provided in this section will transform them into this:

http://domain.tld/subdirectory/post-title/

Locate your blog’s permalink htaccess rules. Then, place the following code directly after the line containing the RewriteBasedirective:

# remove year info from permalinks for blog in subdirectory RewriteRule ^([0-9]{4})/([^/]+)/?$ http://domain.tld/subdirectory/$2/ [R=301,L]

Remember to edit the “domain.tld/subdirectory” to match that of your own. No other changes are necessary. Test like crazy. After verifying that everything works as intended, sit back and enjoy your new optimized permalinks!

I wanted to change my Word­Press perma­link set­tings from being date and name based to some­thing which was sim­ply name based.

Why did I want to do this?

  1. Perma­links would be shorter; remov­ing the date infor­ma­tion results in URIs which are eleven char­ac­ters shorter.
  2. Perma­links would not only be more read­able, they would be more mean­ing­ful to humans; dates aren’t as impor­tant as the con­tent the page itself con­tains. If pre­sented with a list of perma­links from this site, users should have a good idea of what they’ll get when they click them. There’s no rea­son to muddy those waters by inject­ing date infor­ma­tion into the mix, dilut­ing the impact of the domain and post slug.
  3. The con­tent part of the perma­links (domain name + post slug) would carry more impact in var­i­ous search engines, with­out date infor­ma­tion (that may or may not be rec­og­nized as dates by search engine algo­rithms) dilut­ing the value of what­ever key­words may be present.
  4. Yes, this is a blog, and yes infor­ma­tion is posted chrono­log­i­cally; how­ever, this isn’t a novel, and the mate­r­ial here need not be read in a chrono­log­i­cal man­ner in order to under­stand it. Hav­ing date-based perma­links cre­ates the illu­sion that the date the mate­r­ial was posted is more impor­tant than it really is. Besides, posts are dis­played with the date any­way; why give it to the user twice when I’m bet­ting the date included with the post is glanced over by most users.

404! You broke the Inter­net! File not found! It’s the end of the world as you know it!

But don’t worry, you can feel fine about it because I have a solu­tion, pro­vided that you are using Apache as your server envi­ron­ment (chances are, you are) and mod_alias is enabled (I’m unsure how com­mon it is). We’ll be fix­ing our prob­lem using the RedirectMatch direc­tive of mod_alias.

  1. Down­load your blog’s pri­mary .htaccess file; most likely, it’ll be in the same direc­tory as your blog’s wp-config.php file.
  2. Open it up, and find Word­Press’ perma­links code. If you’ve never edited your .htaccess file before, it will most likely be the only code present. The code looks like this, give or take a line break:
    # BEGIN WordPress <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule> # END WordPress
  3. Before that Word­Press code, you’ll want to add one of the following:
    • If your Word­Press is installed in your domain’s root direc­tory (e.g., example.com/wp-config.php), add this code, adjust­ing example.com to your proper domain name:
      RedirectMatch permanent ^/[0-9]{4}/[0-9]{2}/[0-9]{2}/([a-z0-9\-/]+) http://example.com/$1
    • If your Word­Press is installed in a sub­di­rec­tory of your domain (e.g.,example.com/blog/wp-config.php), add this code, adjust­ing example.com/blog to your proper domain name and directory:
      RedirectMatch permanent /blog/[0-9]{4}/[0-9]{2}/[0-9]{2}/([a-z0-9\-/]+) http://example.com/blog/$1
  4. Save & upload the .htaccess file, and revisit one of your older date-based perma­links. If all went accord­ing to plan, you should be seam­lessly redi­rected to your new name-based permalinks.

That’s all there is to it! If any­one can pro­vide a bet­ter means, I’m open to it, but I’ve tested this out, and it works exactly as I want it to — posts work, paged posts work, pages work, and so on. I can’t promise this won’t break any Word­Press plu­g­ins, so be pre­pared to give up this method (which is as easy as delet­ing what­ever you add to .htaccess and switch­ing your perma­link struc­ture back).

For the curi­ous, this is what the code above does, piece by piece:

Redi­rect­Match
Begins the call to Apache’s mod_alias module.
per­ma­nent
Sends the client (i.e., the user, whether human or robot) instruc­tion that this redi­rect is a per­ma­nent redi­rect and that the old address is invalid and should be from now on replaced with the new.
^/[0–9]{4}
The address which we’re try­ing to match begins with a four-digit year stamp, so we first look for a slash fol­lowed by four numbers.
/[0–9]{2}
The address will next have a two-digit month stamp, so we check for another slash fol­lowed by two numbers.
/[0–9]{2}
The address will next have a two-digit day stamp, so we check for yet another slash fol­lowed again by two numbers.
/([a-z0-9\-/]*)
The address will then have the post slug, which can con­sist of a vari­ety of low­er­case let­ters, num­bers, and dashes. So we look for any num­ber of those. A post slug may include addi­tional infor­ma­tion after it, such as a page num­ber on paged posts, so we include any num­ber of slashes in our search as well. It’s enclosed in paren­the­ses so we can cap­ture that data and use it in the next section.
http://example.com/$1
This is the loca­tion of where we want to redi­rect to. The $1 at the end will be replaced by what­ever was matched by the code in paren­the­ses in the pre­vi­ous statement.
Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s