A Wordpress broken link helper
Just an example.
For wordpress ppl who changed their perm link.
- <?php
- 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');
- }
- ?>
- <html><head>
- <meta http-equiv="refresh" content="5;url=<?php echo $link;?>" / > <title>Page has moved</title> </head>
- <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>
- </body></html>
- <?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.
Lighttpd url-redirect for Wordpress Permlink changes
I changed my wordpress blog entry permlink format. But some link to my blog exists on Internet doesn't work which results in a HTTP 404 error.
Why redirect is better then rewrite
Someone may have the same question, but take it easy, it depends on your decision.
But rewrite has it's benefits also, existed link will work without any changes. RSS feed, your live writer(maybe you prefer Word2007? :P) will still work, no need to change the configuration.
I want all visitors include search engine spiders get to know that the permlink changes, that's why I choose redirect, not rewrite.
Draft conf, simple and ugly
For example, http://feuvan.net/wordpress/?p=107 now should be http://blog.feuvan.net/index.php/2007/03/10/win-ce-platform-builder-60-evalutation-offline-download.html, then I have to configure my lighttpd configure like these.
$HTTP["url"] =~ "^/wordpress" {
url.redirect =(
"^/wordpress/(.*)$" => "http://blog.feuvan.net/index.php/$1"
)
}
But some links like http://feuvan.net/wordpress/index.php?p=56 will be redirected to http://blog.feuvan.net/index.php/index.php?p=56, it's ugly, er......
More clean conf file
So here we have a newer configure file:
$HTTP["url"] =~ "^/wordpress" {
url.redirect =(
"^/wordpress/index.php?(.*)$" => "http://blog.feuvan.net/index.php/$1",
"^/wordpress/(.*)$" => "http://blog.feuvan.net/index.php/$1"
)
}
Now the old link will be redirected to http://blog.feuvan.net/index.php/?p=56.
Furthermore
Wait, why not redirect to the new permlink directly?
Maybe I can throw out a solution.
NOTE:
You should enable mod_redirect to make the configuration works for you.
The article is specified to lighttpd 1.4.x series.












