A WordPress broken link helper

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


require_once( dirname(__FILE__) . '/wp-load.php' );

global $wpdb;

$uri = $_SERVER["REQUEST_URI"];
$pattern = "/^\/(\d{4})\/(\d{2})\/(\d{2})\/([^\d].*|\d+[.^-]*).html$/";
if (preg_match($pattern, $uri, $match) == 1)
{
$year = $match[1];
$month = $match[2];
$mday = $match[3];
$post_name = $match[4];

if ((string)(int)$post_name == $post_name) {
$id = (int)$post_name;
$link = get_permalink($id);
if ($link)
wp_redirect($link, '301'); // Permanent redirect
}
$query = "SELECT post_id FROM $wpdb->postmeta, $wpdb->posts WHERE ID=post_id “.
“AND YEAR(post_date) = $year AND MONTH(post_date) = $month AND DAYOFMONTH(post_date) = $mday “.
“AND post_name = \”$post_name\” LIMIT 1″;

$id = (int)$wpdb->get_var($query);
$link = get_permalink($id);
if ($link) wp_redirect($link, ’301′); // Permanent redirect

$query = “SELECT ID FROM $wpdb->posts WHERE “.
“YEAR(post_date) = $year AND MONTH(post_date) = $month AND DAYOFMONTH(post_date) = $mday “.
“AND post_name = \”$post_name\” LIMIT 1″;
$id = (int)$wpdb->get_var($query);
$link = get_permalink($id);
if ($link) wp_redirect($link, ’301′); // Permanent redirect

$link = “/$year/$month/$mday/”;

if (strstr($_SERVER["HTTP_USER_AGENT"], “Mozilla”) === FALSE) // if it’s not a user
{
wp_redirect($link, ’302′);
}
?>

“>the date archive page. It would be nice if you can report broken links to feuvan#feuvan.net.



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.

3 Responses to A WordPress broken link helper

  1. Sunshow says:

    wow, your blog was reloaded!

  2. feuvan says:

    看来WP Hashcash也挡不住nb的spam

  3. bianbian says:

    wokao, quan-shi English, niubility!

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> <pre lang="" line="" escaped="" highlight="">