| 1 | <?php |
| 2 | // Based on "Permalink Redirect" from Scott Yang and "Enforce www. Preference" by Mark Jaquith |
| 3 | |
| 4 | function redirect_canonical() { |
| 5 | global $wp_rewrite, $posts; |
| 6 | |
| 7 | if ( is_feed() || is_trackback() || is_search() || is_comments_popup() ) |
| 8 | return; |
| 9 | |
| 10 | // build the URL in the address bar |
| 11 | $requested_url = ( isset($_SERVER['HTTPS'] ) && strtolower($_SERVER['HTTPS']) == 'on' ) ? 'https://' : 'http://'; |
| 12 | $requested_url .= $_SERVER['HTTP_HOST']; |
| 13 | $requested_url .= $_SERVER['REQUEST_URI']; |
| 14 | |
| 15 | $original = @parse_url($requested_url); |
| 16 | if ( false === $original ) |
| 17 | return; |
| 18 | |
| 19 | $redirect = $original; |
| 20 | $redirect_url = false; |
| 21 | |
| 22 | // These tests give us a WP-generated permalink |
| 23 | if ( is_404() ) { |
| 24 | $redirect_url = redirect_guess_404_permalink(); |
| 25 | } elseif ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() ) { |
| 26 | // rewriting of old ?p=X, ?m=2004, ?m=200401, ?m=20040101 |
| 27 | if ( is_single() && get_query_var('p') ) { |
| 28 | if ( $redirect_url = get_permalink(get_query_var('p')) ) |
| 29 | $redirect['query'] = remove_query_arg('p', $redirect['query']); |
| 30 | } elseif ( is_page() && get_query_var('page_id') ) { |
| 31 | if ( $redirect_url = get_permalink(get_query_var('page_id')) ) |
| 32 | $redirect['query'] = remove_query_arg('page_id', $redirect['query']); |
| 33 | } elseif ( get_query_var('m') && ( is_year() || is_month() || is_day() ) ) { |
| 34 | $m = get_query_var('m'); |
| 35 | switch ( strlen($m) ) { |
| 36 | case 4: // Yearly |
| 37 | $redirect_url = get_year_link($m); |
| 38 | break; |
| 39 | case 6: // Monthly |
| 40 | $redirect_url = get_month_link( substr($m, 0, 4), substr($m, 4, 2) ); |
| 41 | break; |
| 42 | case 8: // Daily |
| 43 | $redirect_url = get_day_link(substr($m, 0, 4), substr($m, 4, 2), substr($m, 6, 2)); |
| 44 | break; |
| 45 | } |
| 46 | if ( $redirect_url ) |
| 47 | $redirect['query'] = remove_query_arg('m', $redirect['query']); |
| 48 | // now moving on to non ?m=X year/month/day links |
| 49 | } elseif ( is_day() && get_query_var('year') && get_query_var('monthnum') && get_query_var('day') ) { |
| 50 | if ( $redirect_url = get_day_link(get_query_var('year'), get_query_var('monthnum'), get_query_var('day')) ) |
| 51 | $redirect['query'] = remove_query_arg(array('year', 'monthnum', 'day'), $redirect['query']); |
| 52 | } elseif ( is_month() && get_query_var('year') && get_query_var('monthnum') ) { |
| 53 | if ( $redirect_url = get_month_link(get_query_var('year'), get_query_var('monthnum')) ) |
| 54 | $redirect['query'] = remove_query_arg(array('year', 'monthnum'), $redirect['query']); |
| 55 | } elseif ( is_year() && get_query_var('year') ) { |
| 56 | if ( $redirect_url = get_year_link(get_query_var('year')) ) |
| 57 | $redirect['query'] = remove_query_arg('year', $redirect['query']); |
| 58 | } elseif ( is_category() && get_query_var('cat') ) { |
| 59 | if ( $redirect_url = get_category_link(get_query_var('cat')) ) |
| 60 | $redirect['query'] = remove_query_arg('cat', $redirect['query']); |
| 61 | } elseif ( is_author() && get_query_var('author') ) { |
| 62 | $author = get_userdata(get_query_var('author')); |
| 63 | if ( false !== $author && $redirect_url = get_author_link(false, $author->ID, $author->user_nicename) ) |
| 64 | $redirect['query'] = remove_query_arg('author', $redirect['author']); |
| 65 | } |
| 66 | |
| 67 | // paging |
| 68 | if ( is_paged() && strpos($redirect['query'], 'paged=') !== false ) { |
| 69 | if ( $paged = get_query_var('paged') ) { |
| 70 | if ( $paged > 1 ) { |
| 71 | if ( !$redirect_url ) |
| 72 | $redirect_url = $requested_url; |
| 73 | $paged_redirect = @parse_url($redirect_url); |
| 74 | $paged_redirect['path'] = preg_replace('|/index.php/?$|', '/', $paged_redirect['path']); |
| 75 | $paged_redirect['path'] = trailingslashit($paged_redirect['path']); |
| 76 | if ( $wp_rewrite->using_index_permalinks() && strpos($paged_redirect['path'], '/index.php/') === false ) |
| 77 | $paged_redirect['path'] .= 'index.php/'; |
| 78 | $paged_redirect['path'] .= user_trailingslashit("page/$paged"); |
| 79 | $redirect_url = $paged_redirect['scheme'] . '://' . $paged_redirect['host'] . $paged_redirect['path']; |
| 80 | } |
| 81 | $redirect['query'] = remove_query_arg('paged', $redirect['query']); |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | // tack on any additional query vars |
| 87 | if ( $redirect_url && $redirect['query'] ) |
| 88 | $redirect_url .= '?' . $redirect['query']; |
| 89 | |
| 90 | if ( !$redirect_url ) { // we're only going down this road if we don't have a WP-generated link from above |
| 91 | |
| 92 | // www.example.com vs example.com |
| 93 | $user_home = parse_url(get_option('home')); |
| 94 | $redirect['host'] = $user_home['host']; |
| 95 | |
| 96 | // trailing /index.php or /index.php/ |
| 97 | $redirect['path'] = preg_replace('|/index.php/?$|', '/', $redirect['path']); |
| 98 | |
| 99 | // need to strip off the query string |
| 100 | if ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() && !is_404() && (!is_home() || ( is_home() && is_paged() ) ) ) |
| 101 | $redirect['path'] = user_trailingslashit($redirect['path']); |
| 102 | |
| 103 | if ( array($original['host'], $original['path'], $original['query']) !== array($redirect['host'], $redirect['path'], $redirect['query']) ) { |
| 104 | $redirect_url = $redirect['scheme'] . '://' . $redirect['host'] . $redirect['path']; |
| 105 | if ( $redirect['query'] ) |
| 106 | $redirect_url .= '?' . $redirect['query']; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | if ( $redirect_url && $redirect_url != $requested_url ) { |
| 111 | // var_dump($redirect_url); die(); |
| 112 | $redirect_url = apply_filters('redirect_canonical', $redirect_url, $requested_url); |
| 113 | wp_redirect($redirect_url, 301); |
| 114 | exit(); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | function redirect_guess_404_permalink() { |
| 119 | global $wp_query, $wpdb; |
| 120 | if ( !get_query_var('name') ) |
| 121 | return false; |
| 122 | |
| 123 | $where = "post_name LIKE '" . $wpdb->escape(get_query_var('name')) . "%'"; |
| 124 | |
| 125 | // if any of year, monthnum, or day are set, use them to refine the query |
| 126 | if ( get_query_var('year') ) |
| 127 | $where .= " AND YEAR(post_date) = '" . $wpdb->escape(get_query_var('year')) . "'"; |
| 128 | if ( get_query_var('monthnum') ) |
| 129 | $where .= " AND MONTH(post_date) = '" . $wpdb->escape(get_query_var('monthnum')) . "'"; |
| 130 | if ( get_query_var('day') ) |
| 131 | $where .= " AND DAYOFMONTH(post_date) = '" . $wpdb->escape(get_query_var('day')) . "'"; |
| 132 | |
| 133 | $post_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE $where AND post_status = 'publish'"); |
| 134 | if ( !$post_id ) |
| 135 | return false; |
| 136 | return get_permalink($post_id); |
| 137 | } |
| 138 | |
| 139 | add_action('template_redirect', 'redirect_canonical'); |
| 140 | |
| 141 | ?> |
| 142 | No newline at end of file |