regenerate_rules(); } $options = array('feedburner', 'feedburnerbrand', 'hostname', 'oldstruct', 'skip', 'newpath'); $optionvars = array(); foreach ($options as $option) { $$option = get_option("permalink_redirect_$option"); if (!$$option) { $$option = ($option == 'feedburnerbrand') ? 'feeds.feedburner.com' : ''; } if ($wp_version < '2' && !$$option) { add_option("permalink_redirect_$option", $$option); } $optionvars[] = "permalink_redirect_$option"; } $home = parse_url(get_option('home')); ?>

Permalink Redirect Manager

Paths to be skipped

Separate each entry with a new line. Matched with regular expression.

Path pairs to redirect from and to

Separate each entry with a new line. Each line is [from]<spaces>[to].

Old Permalink Structures:
Available tags. One Permalink Structure per line. Current permalink structure:
FeedBurner Redirect: http:///
Hostname Redirect: checked="checked"/> Redirect if hostname is not .

execute2($wp_query); } function execute2($query, $testold=true) { $req_uri = $_SERVER['REQUEST_URI']; if ($query->is_trackback || $query->is_search || $query->is_comments_popup || $query->is_robots || $this->is_skip($req_uri)) { return; } $this->redirect_newpath($req_uri); $this->redirect_feedburner($query); if ($query->is_404) { if ($testold) { $this->redirect_old_permalink($req_uri); } return; } if (($req_uri = @parse_url($_SERVER['REQUEST_URI'])) === false) { return; } $req_path = $req_uri['path']; $new_uri = $this->guess_permalink($query); if (!$new_uri) { return; } $permalink = @parse_url($new_uri); // WP2.1: If a static page has been set as the front-page, we'll get // empty string here. if (!$permalink['path']) { $permalink['path'] = '/'; } if (($req_path != $permalink['path']) || $this->check_hostname()) { wp_redirect($new_uri, 301); } } function guess_permalink($query) { $haspost = count($query->posts) > 0; if (get_query_var('m')) { // Handling special case with '?m=yyyymmddHHMMSS' // Since there is no code for producing the archive links for // is_time, we will give up and not trying any redirection. $m = preg_replace('/[^0-9]/', '', get_query_var('m')); switch (strlen($m)) { case 4: // Yearly $link = get_year_link($m); break; case 6: // Monthly $link = get_month_link(substr($m, 0, 4), substr($m, 4, 2)); break; case 8: // Daily $link = get_day_link(substr($m, 0, 4), substr($m, 4, 2), substr($m, 6, 2)); break; default: return false; } } elseif (($query->is_single || $query->is_page) && $haspost) { $post = $query->posts[0]; $link = get_permalink($post->ID); $page = get_query_var('page'); if ($page && $page > 1) $link = trailingslashit($link) . "$page/"; // WP2.2: In Wordpress 2.2+ is_home() returns false and is_page() // returns true if front page is a static page. if ($query->is_page && ('page' == get_option('show_on_front')) && $post->ID == get_option('page_on_front')) { $link = trailingslashit($link); } } elseif ($query->is_author && $haspost) { global $wp_version; if ($wp_version >= '2') { $author = get_userdata(get_query_var('author')); if ($author === false) return false; $link = get_author_link(false, $author->ID, $author->user_nicename); } else { // XXX: get_author_link() bug in WP 1.5.1.2 // s/author_nicename/user_nicename/ global $cache_userdata; $userid = get_query_var('author'); $link = get_author_link(false, $userid, $cache_userdata[$userid]->user_nicename); } } elseif ($query->is_category && $haspost) { $link = get_category_link(get_query_var('cat')); } elseif ($query->is_day && $haspost) { $link = get_day_link(get_query_var('year'), get_query_var('monthnum'), get_query_var('day')); } elseif ($query->is_month && $haspost) { $link = get_month_link(get_query_var('year'), get_query_var('monthnum')); } elseif ($query->is_year && $haspost) { $link = get_year_link(get_query_var('year')); } elseif ($query->is_home) { // WP2.1: Handling "Posts page" option. In WordPress 2.1 is_home() // returns true and is_page() returns false if home page has been // set to a page, and we are getting the permalink of that page // here. if ((get_option('show_on_front') == 'page') && ($pageid = get_option('page_for_posts'))) { $link = trailingslashit(get_permalink($pageid)); } else { $link = trailingslashit(get_option('home')); } } else { return false; } if ($query->is_paged) { $paged = get_query_var('paged'); if ($paged) $link = trailingslashit($link) . "page/$paged/"; } if ($query->is_feed) { $link = trailingslashit($link) . 'feed/'; } return $link; } function is_feedburner() { return strncmp('FeedBurner/', $_SERVER['HTTP_USER_AGENT'], 11) == 0; } function is_skip($path) { $permalink_redirect_skip = get_option('permalink_redirect_skip'); $permalink_redirect_skip = explode("\n", $permalink_redirect_skip); // Apply 'permalink_redirect_skip' filter so other plugins can // customise the skip behaviour. (Denis de Bernardy @ 2006-04-23) $permalink_redirect_skip = apply_filters('permalink_redirect_skip', $permalink_redirect_skip); foreach ($permalink_redirect_skip as $skip) { $skip = trim($skip); if ($skip && ereg($skip, $path)) return true; } return false; } function redirect_feedburner($query) { // Check whether we need to do redirect for FeedBurner. // NOTE this might not always get executed. For feeds, // WP::send_headers() might send back a 304 before template_redirect // action can be called. global $withcomments; if ($query->is_feed && !$query->is_archive && !$withcomments) { if (($feedburner = get_option('permalink_redirect_feedburner')) && (strncmp('FeedBurner/', $_SERVER['HTTP_USER_AGENT'], 11) != 0)) { $brand = get_option('permalink_redirect_feedburnerbrand'); $brand = $brand ? $brand : 'feeds.feedburner.com'; wp_redirect("http://$brand/$feedburner", 302); } } } // Static page redirect contributed by Sergey Menshikov. function redirect_newpath($path) { if ($newpathlist = get_option('permalink_redirect_newpath')) { $newpathlist = explode("\n", $newpathlist); foreach ($newpathlist as $newpath) { $pair = preg_split('/\s+/', trim($newpath)); if ($pair[0] == $path) { wp_redirect($pair[1], 301); } } } } /** * Called when the main execute function gets a 404 to check against old * permalink structures and perform redirect if an old post can be * matched. */ function redirect_old_permalink($req_uri) { global $wp_query, $wp_rewrite; global $wp_version; $rules = get_option('permalink_redirect_rules'); if (!$rules) { return; } // Backing up the rewrite object for you, imperative programmers! $wp_rewrite_old = $wp_rewrite; // Unsetting the globals. Argh! Evil global variables! foreach ($wp_query->query_vars as $key => $val) { unset($GLOBALS[$key]); } // Going through the rules. foreach ($rules as $rules2) { $wp2 = new WP(); $wp_rewrite = new YLSY_Rewrite(); $wp_rewrite->index = $wp_rewrite_old->index; $wp_rewrite->rules = $rules2; $wp2->parse_request(); if (isset($wp2->query_vars['error']) && ($wp2->query_vars['error'] == 404)) { continue; } $query = new WP_Query(); if ($wp_version >= '2.1') { $posts = $query->query($wp2->query_vars); } else { $wp2->build_query_string(); $posts = $query->query($wp2->query_string); } if (count($posts) > 0) { $wp_rewrite = $wp_rewrite_old; $this->execute2($query, false); return; } } // Restoring global variables. We don't bother to reset the other // variables as we are going to do a 404 anyway. $wp_rewrite = $wp_rewrite_old; } /** * This function is called after someone saved the old permalink * structure. It will create cached version of rewrite rules from the * old structure. */ function regenerate_rules() { global $wp_rewrite; $oldstruct = get_option('permalink_redirect_oldstruct'); if ($oldstruct) { $rules = array(); $oldstruct = explode("\n", $oldstruct); foreach ($oldstruct as $item) { $rules2 = $wp_rewrite->generate_rewrite_rule(trim($item), false, false, false, true); $rules3 = array(); foreach ($rules2 as $match => $query) { $query = preg_replace('/\$(\d+)/', '\$matches[\1]', $query); $rules3[$match] = $query; } $rules[] = $rules3; } update_option('permalink_redirect_rules', $rules); } else { delete_option('permalink_redirect_rules'); } } } /** * I am a dummy class to simulate the WP_Rewite class, but only has one * method implemented. */ class YLSY_Rewrite { function wp_rewrite_rules() { return $this->rules; } } if (!function_exists('wp_redirect')) { function wp_redirect($location, $status=302) { global $is_IIS; $location = apply_filters('wp_redirect', $location, $status); $status = apply_filters('wp_redirect_status', $status, $location); if (!$location) return false; if (function_exists('wp_sanitize_redirect')) { $location = wp_sanitize_redirect($location); } if ($is_IIS) { header("Refresh: 0;url=$location"); } else { status_header($status); header("Location: $location"); } } } $_permalink_redirect = new YLSY_PermalinkRedirect(); add_action('admin_menu', array($_permalink_redirect, 'admin_menu')); add_action('template_redirect', array($_permalink_redirect, 'execute'));