A WordPress broken link helper

Just an example.
For wordpress ppl who changed their perm link.

  1. <?php
  2. require_once( dirname(__FILE__) . '/wp-load.php' );
  3.  
  4. global $wpdb;
  5.  
  6. $uri = $_SERVER["REQUEST_URI"];
  7. $pattern = "/^\/(\d{4})\/(\d{2})\/(\d{2})\/([^\d].*|\d+[.^-]*).html$/";
  8. if (preg_match($pattern, $uri, $match) == 1)
  9. {
  10.     $year = $match[1];
  11.     $month = $match[2];
  12.     $mday = $match[3];
  13.     $post_name = $match[4];
  14.  
  15.     if ((string)(int)$post_name == $post_name) {
  16.         $id = (int)$post_name;
  17.         $link = get_permalink($id);
  18.         if ($link)
  19.             wp_redirect($link, '301'); // Permanent redirect
  20.     }
  21.     $query = "SELECT post_id FROM $wpdb->postmeta, $wpdb->posts WHERE ID=post_id ".
  22.         "AND YEAR(post_date) = $year AND MONTH(post_date) = $month AND DAYOFMONTH(post_date) = $mday ".
  23.         "AND post_name = \"$post_name\" LIMIT 1";
  24.  
  25.     $id = (int)$wpdb->get_var($query);
  26.     $link = get_permalink($id);
  27.     if ($link) wp_redirect($link, '301'); // Permanent redirect
  28.  
  29.     $query = "SELECT ID FROM $wpdb->posts WHERE ".
  30.         "YEAR(post_date) = $year AND MONTH(post_date) = $month AND DAYOFMONTH(post_date) = $mday ".
  31.         "AND post_name = \"$post_name\" LIMIT 1";
  32.     $id = (int)$wpdb->get_var($query);
  33.     $link = get_permalink($id);
  34.         if ($link) wp_redirect($link, '301'); // Permanent redirect
  35.  
  36.     $link = "/$year/$month/$mday/";
  37.  
  38.     if (strstr($_SERVER["HTTP_USER_AGENT"], "Mozilla") === FALSE) // if it's not a user
  39.     {
  40.         wp_redirect($link, '302');
  41.     }
  42. ?>
  43. <html><head>
  44. <meta http-equiv="refresh" content="5;url=<?php echo $link;?>" / > <title>Page has moved</title> </head>
  45. <body><p>The permant link changes, however <i>broken link helper</i> can't solve it automaticlly, it will redirect you to <a href="<?php echo $link;?>">the date archive page</a>. It would be nice if you can report broken links to feuvan#feuvan.net.</p>
  46. </body></html>
  47. <?php } ?>

Corresponding nginx configuration block:

server {
listen 80;
server_name blog.feuvan.net;

root /home/feuvan/wwwdata/blog;
index index.php;
if (-e $request_filename) {
break;
}

rewrite ^/index.php/(.+)$ /index.php?q=$1 last;
rewrite “^/([0-9]{4})/([0-9]{2})/([0-9]{2})/([^\d].*|\d+[.^-]*).html” /brokenlinkhelper.php last;
#rewrite ^/(.+)$ /index.php?q=$1 last;
rewrite ^/ /index.php last;

location ~ \.php$ {
fastcgi_pass unix:/tmp/php.sock;
}
}

Hope it helps.

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)
This entry was posted in Default and tagged , . Bookmark the permalink.

7 Responses to A WordPress broken link helper

  1. Sunshow says:

    wow, your blog was reloaded!

  2. Pauline says:

    Oh, it is so difficult for me, I didn’t have such mistake, but I think, if I would have, I cant correct it myself.

  3. Joel says:

    It is difficult to understand the cause but I think that now everyone cope with the emergence of such a mistake!

  4. feuvan says:

    看来WP Hashcash也挡不住nb的spam

  5. Mickey says:

    I am a beginner and for me this bug is not easy to fix. But I think need to try to receive.

  6. bianbian says:

    wokao, quan-shi English, niubility!

  7. David says:

    This is all a bit difficult for me because I am still only beginning to understand all this!

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>

Powered by WP Hashcash