catching stragglers

If you’ve written something that intercepts HTTP requests for missing pages and makes an educated guess where they should go, do you mind sharing it?

This entry was posted in blogdom, me!me!me!, neep-neep. Bookmark the permalink.

6 Responses to catching stragglers

  1. Doug says:

    I used mod_rewrite in my .htaccess file, in order to deal with a change in URL format. What used to be index/P175 became weblog.php?id=P175, along with a number of similar changes.

    That part of my .htaccess looks something like this:


    RewriteEngine On
    RewriteRule ^index/(.*)/$ /weblog.php?id=$1 [R=301,L]
    RewriteRule ^index/(.*)$ /weblog.php?id=$1 [R=301,L]
    RewriteRule ^comments/(.*)/$ /comments.php?id=$1 [R=301,L]
    RewriteRule ^comments/(.*)$ /comments.php?id=$1 [R=301,L]
    RewriteRule ^more/(.*)/$ /more.php?id=$1 [R=301,L]
    RewriteRule ^more/(.*)$ /more.php?id=$1 [R=301,L]
    RewriteRule ^archives/(.*)/$ /archives.php?id=$1 [R=301,L]
    RewriteRule ^archives/(.*)$ /archives.php?id=$1 [R=301,L]
    RewriteRule ^archivesum/(.*)/$ /archivesum.php?id=$1 [R=301,L]
    RewriteRule ^archivesum/(.*)$ /archivesum.php?id=$1 [R=301,L]

    I probably have something out of whack in there — I am a complete tyro at writing rewrite rules — but it mostly seems to work for me.

  2. Anton says:

    Hm, that does most of what I have in mind. Is there a syntax to convert an unknown number of “-” to “%20”?

    My ideal redirector would go a little bit further and infer, from the referral source, what specific item was sought.

  3. Doug says:

    I think that both should be possible. That’s just a “regular expression”, so something like “–*” should match a series of one or more hyphens. (I think that “-+” would, too.)

    And you should be able to do different things depending on referrer by using the “RewriteCond %{HTTP_REFERER}” rules.

    But like I said, I’m a complete tyro at mod_rewrite, so I’m not in a position to give any definitive answers or suggestions.

  4. Anton says:

    It’s subtler than a regular expression. I want to convert, for example, /blog/2004/06/how-many-readers-have-i-in-this.htm to wp/?s=how+many+readers+have+i+in+this&submit=Search — and I know no syntax to catch that.

  5. Doug says:

    I’m way beyond the limits of my mod_rewrite expertise here, but maybe it could be handled by something like:

    RewriteRule ^(.*)-(.*)$ $1+$2 [N]
    RewriteRule ^blog/[0-9]+/[0-9]+/(.*)\.htm$ wp/?s=$1&submit=Search

    The “N” causes the RewriteRules to be restarted from the beginning, so you’d want this near the top of the list of rules.

    You could also do it using “[R]” rather than “[N]”. That should cause the user’s browser to re-request with the modified URL, over and over as each – is replaced with a +, until finally there are no hyphens left. I’m not sure there’s any advantage to doing it that way, though, and there’s the disadvantage of having the browser re-issue the request a bunch of times. (I used R=301 in my own rules because the pages had moved and I wanted the user to bookmark the new addresses, not the old ones.)

    Another alternative, if you’ll accept a limit on the number of hyphens removed, is to use rule chaining. This sequence will (I think) remove up to eight hyphens:

    RewriteRule ^([^-]*)-(.*)$ $1+$2 [C]
    RewriteRule ^([^-]*)-(.*)$ $1+$2 [C]
    RewriteRule ^([^-]*)-(.*)$ $1+$2 [C]
    RewriteRule ^([^-]*)-(.*)$ $1+$2 [C]
    RewriteRule ^([^-]*)-(.*)$ $1+$2 [C]
    RewriteRule ^([^-]*)-(.*)$ $1+$2 [C]
    RewriteRule ^([^-]*)-(.*)$ $1+$2 [C]
    RewriteRule ^([^-]*)-(.*)$ $1+$2

    Actually it should work without the chaining, too. The chaining just saves a bit of mod_write a bit of effort because it will quit looking for hyphens after the first time it doesn’t find one.

    By the way, the second RewriteRule above – the one with the query string – has the salutary effect of causing any user-supplied query string to be discarded. This is probably a bit of a security benefit.

    There isn’t much you can’t do with mod_rewrite… once you figure out HOW to do it. The official docs call it “the Swiss Army Knife of URL manipulation” but also acknowledge, “Despite the tons of examples and docs, mod_rewrite is voodoo. Damned cool voodoo, but still voodoo.”

  6. Anton says:

    My attempt at iterative replacement resulted in what looked like an infinite loop, so I brute-forced it: there are only four such files that I know to have been cited elsewhere.

    In other news, it’s unfortunate that the conversion from Blogger did not preserve the <a name> tags. I mean to restore a few of them by hand.

Leave a Reply

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