Permalink Redirect WordPress Plugin
Moved!
I have moved this project to my new programming/Internet blog — Permalink Redirect WordPress plugin.
About
I am not sure why the WordPress people insist that,
GET /2005/05/29/foo-bar HTTP/1.1
is exactly the same as
GET /2005/05/29/foo-bar/ HTTP/1.1
If you have not spotted the difference, one has a trailing forward slash ‘/’ in the URI and one does not. For one is requesting the content of object ‘foo-bar’ under directory ‘/2005/05/29/’, and another one is requesting the content of the index file under directory ‘/2005/05/29/foo-bar/’. Although many of us would usually ignore the trailing slash, and write out the URL without it, web servers usually will correct this behaviour by replying a 301 permanent redirect to an URL with the trailing slash, if ‘foo-bar’ is indeed a directory.
However, not so with the default mod_rewrite rules generated by WordPress. To save number of rules need to be generated, WordPress short-cuts the regular expression to allow optional trailing slashes at the end of the URL. This results that you can now point to the same entry/archive with or without the trailing slash – which goes against what a search engine would recommend. Here is an extract of Google’s recommendation to the webmasters, under “Quality Guidelines – Specific recommendations”:
Don’t create multiple pages, subdomains, or domains with substantially duplicate content.
Now if I search Google for certain keywords, I sometimes see two results came back from the same domain – same blog entry but one with a trailing slash in its URL, and one does not. For example, mtsend. This is certainly not what I have desired. I used to modify all the mod_rewrite rules generated by WordPress, so that Apache will issue a 301 redirect if the trailing space is missing. However, it is tedious, and not compatible when WordPress upgrades. Moreover, your .htaccess becomes huge and getting unmaintainable.
Therefore, this simple WordPress 1.5+ plugin is written. It compares the request URI, and if it does not match the permalink, a redirect will be sent back instead. It works not only for individual blog entries, but also yearly/monthly/daily and category archives, so that there is always only be one URL for each of your blog entries and archives.
Links to This Article
- One Permalink Only [ Tempus Fugit | TxFx.net ]
- idleMind.org Journal
- Martin Geisler Online
- SteelPixel
- SteelPixel » Blog Archive » Hacking the Permalink Redirect Wordpress Plugin
- MacManX.com » Blog Archive » Active Plugins
- unFocus » permalinks on IIS
- The making of this blog – verbage.net
- mein Weblog und seine Plugins
- GeekFun » Blog Archive » From Movable Type to Wordpress
- Tabakblog » Blog Archive » Weitere Wordpress-Plugins installiert
- Permalink Redirect WordPress Plugin 0.4 | FuCoder.com
- SEO Plugins Wordpress 1.5.2 - SEO Marketing Blog
- A Guide to an SE Optimized Wordpress Setup » @ Ambot ah! [ technology news and reviews ]
- Weblog-Optimierung f
- Los Blog’s de la pagina de UiDoS » Plugins para WordPress
- SEO Plugins für WordPress - BeNatix.com
- SEO para Wordpress
- 睡觉的眼皮
- 282 plugins para WordPress (4ª parte de la L a la Q) | Pichicola.com
- Inspirit Blog switches from Windows based host to Linux based host
- Plugins Para WordPress | Mega Post
- Inicio « mediasur.cl
- Plugins para Wordpress. | Puydi Publicidad 100% efectiva
- webmaster esmuy.com » Blog Archive » Plugins para Wordpress
- Gran colección de plugins Wordpress -- Mundo Inteligente
- Plugins para wordpress | UrbanBlog
- Oloroko Blog – Lista de Plugins para Wordpress
Comments
That’s a possibility – there might be someone who linked the entry without the trailing slash. Googlebot followed the link, and subsequently indexed the same entry again with two totally different URL’s. But I guess my point is, WordPress should handle this situation by doing a redirect to the correct permalink, instead of just serve the content. Googlebot would be happy to take a 301 and follow to the new location.
I’ll see whether that entry goes away from Google’s index when they update the next time.
That is in fact just one example: this plugin will also redirect stuff like http://yourblog.ext/?p=123 to the correct permalink IRI. Thanks for this kick-ass piece of coding, I hope it will get into the core soon.
(off topic : I noticed that there is something weird with something I identify as my IP 2 Nation plugin. The first flag in comments has an alt tag with “from France”, which is my country, and then every flag has the alt tag of its predecessor one. Weird heh :)
Ozh,
Thanks for the spotting! It is indeed a typo in my template. Fixed now.
Everyone,
Thanks for the comments. I’ll try to get that registered as a WordPress plugin today.
I have just one question.
Does it modify /foo-bar and make it /foo-bar/ or does it take /foo-bar/ and make it /foo-bar ??
I mean does it add or remove the slash?
If such a system is used could it solve this problem of mine?
Ajay,
It shall redirect to whatever the permalink is supposed to be, with or without the trailing ‘/’. However, I don’t think it would work if your permalink config does not have the final ‘/’, but then someone request it with one, as I only check whether the permalink is a substring of the request URI, in case there are other things appending to it (/page/, /feed/, etc)
Thanks for the plugin, I too find it important to redirect people to the proper canonical URL.
As far as I figured out, then it doesn’t treat URLs to pages, only to posts. Could it be updated to handle pages too?
Very nice! This fixes a problem that was bugging me last week when I discovered pingbacks had stopped using the permalink structure. It was just an upgrade snafu, fixed easily, but I had left a week or two of pingbacks scattered around the web with an ugly/redundant index.php?p=xyz link. (Not to mention all the duplicate links from people leaving out the trailing slash.)
Tried installing using 1Click install. I get this error:
Fatal Error: Couldn’t download one-click archive from ‘http://scott.yang.id.au/file/php/ylsy_permalink_redirect.zip’
When I activate your plugin, the JÄÅ Preview plugin stops working.
The JÄÅ Preview plugin launches a new browser window with an URL of this type: [blog URL]/?name=preview&idpost=[post ID]. However, if your plugin is activated, the browser is redirected to an URL like this: [blog URL]/archives/[year]/[month]/[day]//. Would you care to check this out, I’ve looked at the code of both your plugin and JÄÅ Preview but even though it looks simple I have no experience in writing WordPress plugins.
Ajay,
I have not tried 1click install yet – might have a look later.
Paulo,
As far as I can see, the preview plugin does nothing but loads the specific post from database and display in the a separate window – which is exactly the same as loading the post from its permalink! Even though permalinks redirect plugin will alter the final URL, I don’t see how it has stopped the prview plugin from working.
Hello,
I found it can not convert something like “/?m=200504″, it should be “/2005/04″ but it always turns to be “/2005/06″ (current month).
I looked a bit further into the problem and I discovered that it had nothing to do with your plugin! :) It was the fact that the post slug was missing that prevented the URL from loading (hence the empty space between the slashes at the end of the address).
Congratulations for your plugin, it’s really useful.
Very nice!
It’s not working entirely for me though.
Could it be that I purposefully do not have trailing slashes on my site? That is, My permalinks are /%year%/%month%/%postname% and not /%year%/%month%/%postname%/
Mark,
The ‘?m=yyyymm’ syntax is not supported, but it should be trivial to add that by looking at how WP_Query is constructed. I’ll look at that tomorrow (need to head to bed soon). Nice blogsite, btw :)
Joen, I have the same permalink structure as you do, and (that part of) the plugin works perfectly on my blog.
I am having that problem too though, but then with category permalinks. Those look like this on my site: http://mathibus.com/archive/topics/php (note the lack of a trailing slash, which is intended). Your plugin seems to redirect that kind of stuff to http://mathibus.com/archive/topics/php/ (with the trailing slash). The thing is, I have mod_rewrite rules in my .htaccess that redirects IRIs that end with a trailing slash (unless the requested file is a directory, of course) to the same IRI without the slash… Which makes your plugin conflict with my .htaccess and thus cause a redirection loop. Any ideas on how to fix this?
If your permalink does not contain a trailing slash, then in the case when a trailing slash has been appended to the REQUEST URI, it should either
1. process the link without redirect, and treat the trailing slash as PATH_INFO, or
2. issue a 404 Not Found.
I am more in favour of (2) – just try that on any static file and a web server would give you back a 404.
Actually Scotty, I can see a Permalink Redirect plugin available. Did you add it to wp-plugins.net?
Also, check out steelpixel’s hack to include pages as well :)
I think I may have found a small bug. If you make any alteration to the post slug in the URL (while this plugin is active), the custom 404 page is not loaded. Instead, you’re either taken back to the main page of the blog, or taken to a blank page.
Scott: I’m trying to figure out whether your plugin will solve a really thorny technical problem I’m having. I migrated my blog from typepad to wp retaining the same domain name richardsilvertein.com. But typepad permalinks have this format: http://www.richardsilvertein.com/tikun_olam/yr/mo/day/i_have_a.html format. Many people are using old format hyperlinks to get to the same posts but in my new blog (via search engines, links in other people’s blogs, etc.). Since I’m using the same domain for my WP blog, they ARE getting to my new blog site. But I can’t figure out a way to use mod_redirect/htaccess to convert those old links into WP’s http://www.richardsilverstein.com/tikun_olam/yr/mo/day/i-have-a/ format (which is the path structure I’m currently using) so that they can not only get to my new site, but to the specific post they’re seeking.
Is there a way your plugin will do this?
ANother ?: when I click your download link I don’t get an actual download. My FF browser merely opens the file in a new browser screen & I see the code. How do I download it?
Richard,
FF – Right click and “Save Link as”…
And you can set your permalink structure like the way TypePad has set up. Go to the Permalinks section under “Options” of WP admin, and use:
/tikun_olam/%year%/%monthnum%/%day%/%postname%.html
as permalinks structure. You’ll need to use the following plugin to use underscores instead of hyphens in permalinks.
Hello scotty,
Another format that does not redirect by the plugin (0.3)
/index.php?paged=3
will not redirect as
/page/3/
Will you add this? Thanks.
Hey guys,
A little late here, but, I found an .htaccess file that adds the trailing slash automatically. I initially searched for this because yahoo automatically strips the trailing slash. :(
Scott – any chance this script could be modified to handle Typepad’s truncation? Typepad truncates titles to 15 characters and then ads a numeral for dupes. I.e. the first instance of a URL could be:
…/example_of_post
and then a second post with an identical first 15 characters would be,
…/example_of_post_1
I want to migrate to Wordpress but have to find a way to maintain my URL’s. Unlike Movable Type, Typepad doesn’t give the option to keep long page names intact.
I came here from a google search for wordpress permalinks. I wanted to know how wordpress ensures that two blog with the same titles have different permalinks? Any ideas?
Add a comment
Gravatar is used. Email address is required but will not be displayed. Please keep your comment on topic. No spamming and/or bad language. First time poster will be moderated. Scott reserves the right to delete/edit your comments.

Interestingly, if you check out the URIs generated by WordPress through a ’standard’ site, you should notice that all the URIs do in fact have a trailing slash on them.
I’d begin to wonder how the results for mtsend have multiple entries. I initially thought, it might have been caused by people linking to that article with and without the trailing slash, which would/might cause two entries in the Google index. However, if you use a link: to the URI without a trailing slash, it returns no results.
Do you have any thoughts how you’d get multiple entries?
Al.