May 29 2005

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.

71 Comments

  1. Alistair Lattimore on 30 May 2005 at 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. scotty on 30 May 2005 at 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. Matt on 31 May 2005 at 5:24 am #

    You should put this in the plugin directory.

  4. Mathias Bynens on 31 May 2005 at 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. Jauhari on 31 May 2005 at 7:05 am #

    I want try this one.

    Thanks for nice plugin

  6. Ozh on 31 May 2005 at 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. Oliver on 31 May 2005 at 8:42 am #

    Looks awesome! Great job!

  8. Pingback: One Permalink Only [ Tempus Fugit | TxFx.net ]

  9. scotty on 31 May 2005 at 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.

  10. Pingback: idleMind.org Journal

  11. Andy Skelton on 31 May 2005 at 2:24 pm #

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

  12. Ajay D'Souza on 31 May 2005 at 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?

  13. scotty on 31 May 2005 at 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)

  14. Norman Rasmussen on 31 May 2005 at 8:22 pm #

    You ROCK!

  15. MacManX on 1 Jun 2005 at 3:53 am #

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

  16. Martin Geisler on 1 Jun 2005 at 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?

  17. Pingback: Martin Geisler Online

  18. Kelson on 1 Jun 2005 at 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.)

  19. Bryan on 1 Jun 2005 at 6:46 am #

    Awesome plugin, thanks Scott!

  20. Ajay D'Souza on 1 Jun 2005 at 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’

  21. Paulo Ribeiro on 1 Jun 2005 at 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.

  22. scotty on 1 Jun 2005 at 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.

  23. Mark on 1 Jun 2005 at 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).

  24. Paulo Ribeiro on 1 Jun 2005 at 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.

  25. Mark on 1 Jun 2005 at 11:58 pm #

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

  26. Joen on 2 Jun 2005 at 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%/

  27. scotty on 2 Jun 2005 at 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 :)

  28. Mathias Bynens on 2 Jun 2005 at 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?

  29. Pingback: SteelPixel

  30. Pingback: SteelPixel » Blog Archive » Hacking the Permalink Redirect Wordpress Plugin

  31. XeroCool on 2 Jun 2005 at 8:13 am #

    Great plugin. Thanks.

  32. scotty on 2 Jun 2005 at 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.

  33. Ajay D'Souza on 2 Jun 2005 at 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 :)

  34. MacManX on 3 Jun 2005 at 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.

  35. Pingback: MacManX.com » Blog Archive » Active Plugins

  36. Jonathan Stanley on 4 Jun 2005 at 12:34 am #

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

  37. Pingback: unFocus » permalinks on IIS

  38. Richard Silverstein on 17 Jun 2005 at 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?

  39. Richard Silverstein on 17 Jun 2005 at 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?

  40. scotty on 18 Jun 2005 at 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

  41. Mark on 22 Jun 2005 at 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.

  42. Pingback: The making of this blog – verbage.net

  43. Recipher on 17 Jul 2005 at 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

  44. Sam Sugar on 26 Jul 2005 at 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.

  45. Pingback: mein Weblog und seine Plugins

  46. Pingback: GeekFun » Blog Archive » From Movable Type to Wordpress

  47. Pingback: Tabakblog » Blog Archive » Weitere Wordpress-Plugins installiert

  48. Pingback: Permalink Redirect WordPress Plugin 0.4 | FuCoder.com

  49. Pingback: SEO Plugins Wordpress 1.5.2 - SEO Marketing Blog

  50. Pingback: A Guide to an SE Optimized Wordpress Setup » @ Ambot ah! [ technology news and reviews ]

  51. Pingback: Weblog-Optimierung f

  52. gices on 18 Dec 2006 at 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?

  53. Pingback: Los Blog’s de la pagina de UiDoS » Plugins para WordPress

  54. Pingback: SEO Plugins für WordPress - BeNatix.com

  55. John on 28 Mar 2007 at 6:15 am #

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

  56. Pingback: SEO para Wordpress

  57. Pingback: 睡觉的眼皮

  58. Pingback: 282 plugins para WordPress (4ª parte de la L a la Q) | Pichicola.com

  59. Pingback: Inspirit Blog switches from Windows based host to Linux based host

  60. Bogdan Radu on 13 Sep 2008 at 1:17 am #

    Awesome plugin, thanks Scott!!

  61. Pingback: Plugins Para WordPress | Mega Post

  62. Sebastian on 31 Jan 2009 at 7:50 am #

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

  63. Pingback: Inicio « mediasur.cl

  64. Pingback: Plugins para Wordpress. | Puydi Publicidad 100% efectiva

  65. Pingback: webmaster esmuy.com » Blog Archive » Plugins para Wordpress

  66. Pingback: Gran colección de plugins Wordpress -- Mundo Inteligente

  67. Pingback: Plugins para wordpress | UrbanBlog

  68. Pingback: Oloroko Blog – Lista de Plugins para Wordpress

  69. Pingback: Descargar Plugins para Wordpress | ZonaFull.Org

  70. Pingback: Plugins para Wordpress «

  71. Tas Walker on 6 Oct 2010 at 8:18 pm #

    My original url was
    http://biblicalgeology.net/blog/archives/527

    I installed the plug in.
    I changed the settings on the WP permalinks settings.
    I edited the permalink on each blog entry to make it shorter and neater.

    The url now is: http://biblicalgeology.net/blog/the-blowhole-kiama/

    When I put the original url in the browser I get a 404 error.

    “Oops! The page you’re looking for can’t be found. Search biblicalgeology.net:
    blog archives 527″

    Can anyone help me fix it? Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>