WordPress 2.2 Permalink Trailing Slash & Duplicate Pages Fix
27 05 2007
Recently, I upgraded this blog to WordPress 2.2 and everything apparently went well as usual. This upgrade is highly recommended as it brings more security patches and better, streamlined DB queries and codes. But problems cropped up when I found that wordpress team has decided to remove the trailing slash that was added to the permalinks by default in older versions.
After upgrade to WP 2.2, I have page, category and archive URLs like agnivo.com/downloads instead of agnivo.com/downloads/ . Now, the indexed pages in search engines have a trailing slash and there are going to be duplicate copies of the same pages indexed and landing in supplementals, hurting my ranking if this problem is allowed to persist. The category RSS feed URLs also broke due to lack of a trailing slash after the category name. So, if you use custom permalink structure, you’re worst affected. All posts URLs are unaffected though as I have a structure like agnivo.com/category/post-title.html
In my opinion, this isn’t a good change implemented by the WP team…So, I searched for hours to make those URLs have trailing slashed like I had before. Finally, I came up with a solution from several sources that I’m sharing with you now :
Edit /wp-includes/link-template.php and make the changes to the user_trailingslashit function as shown below (notice the // comment lines) :
function user_trailingslashit($string, $type_of_url = '') {
global $wp_rewrite;
//if ( $wp_rewrite->use_trailing_slashes )
$string = trailingslashit($string);
//else
// $string = untrailingslashit($string);
// Note that $type_of_url can be one of following:
// single, single_trackback, single_feed, single_paged, feed, category, page, year, month, day, paged
$string = apply_filters('user_trailingslashit', $string, $type_of_url);
return $string;
}
If you have customized permalink for posts ending in .html , .htm, etc. that doesn’t need a trailing slash, then scroll a bit downwards in the same file, find this line and comment it as shown :
//$permalink = user_trailingslashit($permalink, 'single');
Now if you wish to ensure that only valid permalinks are only shown ( eg – agnivo.com/downloads/ even when someone uses agnivo.com/downloads ) then install this plugin and activate it : PermaLink Redirect Plugin
If you wish to redirect all http:// requests to http://www. so that only one copy of your page is indexed by search engines, just goto Options > Permalink Redirect > Hostname Redirect.
Moreover, you can use this plugin to redirect all feed urls to your feedburner feed and all old permalinks to new permalinks if you’ve changed the structure lately. Also, if you have changed the post slug of a particular post, if someone access the post by the older one, it’s automatically redirected to the new URL.
Hope it was useful…feel free to social bookmark and leave comments.
Update : Those of you don’t want to mess around with the WordPress core files, I have put up a plugin for you to do the same job. Just download and install it; no need to modify the core files.
Download Force Trailing Slash Plugin


[...] file and installing the PermaLink Redirect Plugin. I encourage you to visit Agnivo’s entry called “WordPress 2.2 Permalink Trailing Slash & Duplicate Pages Fix†for the detailed [...]
Agnivo you are a life saver!! Being struggling with this issue for some time now.
Very well documented fix. I appreciate your efforts and will let the readers of my blog (YouShouldKnow.com) know about this post!
thanks again
frank
Thank you! Now the duplicate content thingie comes to an end.
Agnivo, I wrote a simple plugin to fix this trailing slash problem:
http://www.fastagent.de/res/download/smart-trailing-slash.zip
It is filter which overrides the function user_trailingslashit. Every permalink will have a trailing slash again, except links that have a file extension (like .html).
Just download, unzip, upload and enjoy
Is this still needed with wp 2.5.1 ? I just noticed search engines crawl my blog, looking for both versions, with and without trailing slash.