Permalink Redirect WordPress Plugin

Moved!

I have moved this project to my new programming/Internet blogPermalink 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.

Category: General | Sun, 29 May 2005 8:15 pm

Links to This Article

  1. Tue, 31 May 2005 9:26 am
    One Permalink Only [ Tempus Fugit | TxFx.net ]
  2. Tue, 31 May 2005 12:34 pm
    idleMind.org Journal
  3. Wed, 1 June 2005 4:45 am
    Martin Geisler Online
  4. Thu, 2 June 2005 1:52 am
    SteelPixel
  5. Thu, 2 June 2005 2:00 am
    SteelPixel » Blog Archive » Hacking the Permalink Redirect Wordpress Plugin
  6. Fri, 3 June 2005 3:34 am
    MacManX.com » Blog Archive » Active Plugins
  7. Sat, 11 June 2005 5:30 pm
    unFocus » permalinks on IIS
  8. Tue, 28 June 2005 3:43 am
    The making of this blog – verbage.net
  9. Sun, 21 August 2005 5:47 am
    mein Weblog und seine Plugins
  10. Sun, 21 August 2005 10:51 am
    GeekFun » Blog Archive » From Movable Type to Wordpress
  11. Fri, 30 September 2005 9:21 pm
    Tabakblog » Blog Archive » Weitere Wordpress-Plugins installiert
  12. Mon, 24 October 2005 11:32 am
    Permalink Redirect WordPress Plugin 0.4 | FuCoder.com
  13. Sun, 1 October 2006 4:51 am
    SEO Plugins Wordpress 1.5.2 - SEO Marketing Blog
  14. Sat, 7 October 2006 10:04 pm
    A Guide to an SE Optimized Wordpress Setup » @ Ambot ah! [ technology news and reviews ]
  15. Sun, 15 October 2006 12:21 pm
    Weblog-Optimierung f
  16. Sun, 24 December 2006 1:38 am
    Los Blog’s de la pagina de UiDoS » Plugins para WordPress
  17. Sun, 14 January 2007 9:24 am
    SEO Plugins für WordPress - BeNatix.com
  18. Thu, 10 May 2007 3:50 am
    SEO para Wordpress
  19. Thu, 5 June 2008 12:51 pm
    睡觉的眼皮
  20. Mon, 18 August 2008 9:33 am
    282 plugins para WordPress (4ª parte de la L a la Q) | Pichicola.com
  21. Thu, 4 September 2008 4:37 am
    Inspirit Blog switches from Windows based host to Linux based host
  22. Mon, 22 December 2008 6:13 am
    Plugins Para WordPress | Mega Post
  23. Thu, 12 February 2009 5:19 pm
    Inicio « mediasur.cl
  24. Sat, 28 February 2009 8:27 am
    Plugins para Wordpress. | Puydi Publicidad 100% efectiva
  25. Fri, 22 May 2009 1:27 am
    webmaster esmuy.com » Blog Archive » Plugins para Wordpress
  26. Wed, 27 May 2009 6:38 am
    Gran colección de plugins Wordpress -- Mundo Inteligente
  27. Sun, 31 May 2009 1:45 am
    Plugins para wordpress | UrbanBlog
  28. Wed, 11 November 2009 7:11 am
    Oloroko Blog – Lista de Plugins para Wordpress

Comments

1.
Avatar for Alistair Lattimore
Posted by Alistair Lattimore on Mon, 30 May 2005 1:50 pm

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.


2.
Avatar for scotty
Posted by scotty on Mon, 30 May 2005 2:06 pm

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.


3.
Avatar for Matt
Posted by Matt on Tue, 31 May 2005 5:24 am

You should put this in the plugin directory.


4.
Avatar for Mathias Bynens
Posted by Mathias Bynens on Tue, 31 May 2005 5:52 am

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.


5.
Avatar for Jauhari
Posted by Jauhari on Tue, 31 May 2005 7:05 am

I want try this one.

Thanks for nice plugin


6.
Avatar for Ozh
Posted by Ozh on Tue, 31 May 2005 8:19 am

(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 :)


7.
Avatar for Oliver
Posted by Oliver on Tue, 31 May 2005 8:42 am

Looks awesome! Great job!


8.
Avatar for scotty
Posted by scotty on Tue, 31 May 2005 10:00 am

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.


9.
Avatar for Andy Skelton
Posted by Andy Skelton on Tue, 31 May 2005 2:24 pm

Ohboyohboyohboy this is going to simplify BOTD coding when it gets into the core. You rock.


Avatar for Ajay D’Souza
Posted by Ajay D'Souza on Tue, 31 May 2005 3:36 pm

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?


Avatar for scotty
Posted by scotty on Tue, 31 May 2005 3:48 pm

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)


Avatar for Norman Rasmussen
Posted by Norman Rasmussen on Tue, 31 May 2005 8:22 pm

You ROCK!


Avatar for MacManX
Posted by MacManX on Wed, 1 June 2005 3:53 am

This is exactly what I’ve been looking for! Well done!


Avatar for Martin Geisler
Posted by Martin Geisler on Wed, 1 June 2005 4:35 am

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?


Avatar for Kelson
Posted by Kelson on Wed, 1 June 2005 5:33 am

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.)


Avatar for Bryan
Posted by Bryan on Wed, 1 June 2005 6:46 am

Awesome plugin, thanks Scott!


Avatar for Ajay D’Souza
Posted by Ajay D'Souza on Wed, 1 June 2005 6:23 pm

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’


Avatar for Paulo Ribeiro
Posted by Paulo Ribeiro on Wed, 1 June 2005 9:05 pm

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.


Avatar for scotty
Posted by scotty on Wed, 1 June 2005 9:50 pm

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.


Avatar for Mark
Posted by Mark on Wed, 1 June 2005 10:40 pm

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).


Avatar for Paulo Ribeiro
Posted by Paulo Ribeiro on Wed, 1 June 2005 11:08 pm

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.


Avatar for Mark
Posted by Mark on Wed, 1 June 2005 11:58 pm

:oops: I missed the trailing slash at comment 23,“/2005/04/″and “/2005/06/″.


Avatar for Joen
Posted by Joen on Thu, 2 June 2005 12:01 am

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%/


Avatar for scotty
Posted by scotty on Thu, 2 June 2005 12:05 am

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 :)


Avatar for Mathias Bynens
Posted by Mathias Bynens on Thu, 2 June 2005 12:42 am

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?


Avatar for XeroCool
Posted by XeroCool on Thu, 2 June 2005 8:13 am

Great plugin. Thanks.


Avatar for scotty
Posted by scotty on Thu, 2 June 2005 1:35 pm

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.


Avatar for Ajay D’Souza
Posted by Ajay D'Souza on Thu, 2 June 2005 7:28 pm

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 :)


Avatar for MacManX
Posted by MacManX on Fri, 3 June 2005 1:35 am

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.


Avatar for Jonathan Stanley
Posted by Jonathan Stanley on Sat, 4 June 2005 12:34 am

Hmmm, just the ticket… more or less. :D


Avatar for Richard Silverstein
Posted by Richard Silverstein on Fri, 17 June 2005 5:37 am

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?


Avatar for Richard Silverstein
Posted by Richard Silverstein on Fri, 17 June 2005 5:40 am

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?


Avatar for scotty
Posted by scotty on Sat, 18 June 2005 10:54 pm

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.

http://codex.wordpress.org/Plugins/Underscore_Permalinks


Avatar for Mark
Posted by Mark on Wed, 22 June 2005 6:34 pm

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.


Avatar for Recipher
Posted by Recipher on Sun, 17 July 2005 2:05 pm

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. :(

http://wordpress.org/support/topic/38441


Avatar for Sam Sugar
Posted by Sam Sugar on Tue, 26 July 2005 12:45 am

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.


Avatar for gices
Posted by gices on Mon, 18 December 2006 9:49 pm

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?


Avatar for John
Posted by John on Wed, 28 March 2007 6:15 am

Hey mate, thanks for the Permalink Redirect WordPress plugin. Verry usefull 4 me. John :-)


Avatar for Bogdan Radu
Posted by Bogdan Radu on Sat, 13 September 2008 1:17 am

Awesome plugin, thanks Scott!!


Avatar for Sebastian
Posted by Sebastian on Sat, 31 January 2009 7:50 am

Thanks, very good plugin. I am searching exactly for such a plugin :)


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.