FVN Tech Blog Coding or Programming in C#/PHP | Linux or Windows Server Backend | New Media | SNS | Misc …

20Jan/097

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)
Tagged as: , 7 Comments

Pages

Recent Posts

Categories

Tags

apache baidu C# Debian dotNET economy gadget GMA Google hulu IE7 Intel jiwai lighttpd Linux Mobile mod_extforward mojiti nginx oscanner OVAL Parallel PHP phproxy politics proxy Python regsvr32 SNS squid subversion trac TV Vista Visual Studio Windows Live Windows Mobile WLM wordpress wow WTL wzdftpd XDebug XML Zend Framework

Archives

Meta

友情链接与广告

Free web proxy
Old friends:

NSClub: