| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
function redirect_canonical($requested_url=null, $do_redirect=true) { |
|---|
| 40 |
global $wp_rewrite, $is_IIS; |
|---|
| 41 |
|
|---|
| 42 |
if ( is_feed() || is_trackback() || is_search() || is_comments_popup() || is_admin() || $is_IIS || ( isset($_POST) && count($_POST) ) || is_preview() || is_robots() ) |
|---|
| 43 |
return; |
|---|
| 44 |
|
|---|
| 45 |
if ( !$requested_url ) { |
|---|
| 46 |
|
|---|
| 47 |
$requested_url = ( isset($_SERVER['HTTPS'] ) && strtolower($_SERVER['HTTPS']) == 'on' ) ? 'https://' : 'http://'; |
|---|
| 48 |
$requested_url .= $_SERVER['HTTP_HOST']; |
|---|
| 49 |
$requested_url .= $_SERVER['REQUEST_URI']; |
|---|
| 50 |
} |
|---|
| 51 |
|
|---|
| 52 |
$original = @parse_url($requested_url); |
|---|
| 53 |
if ( false === $original ) |
|---|
| 54 |
return; |
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 |
$original['path'] = preg_replace('|/index\.php$|', '/', $original['path']); |
|---|
| 58 |
|
|---|
| 59 |
$redirect = $original; |
|---|
| 60 |
$redirect_url = false; |
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 |
if ( is_404() ) { |
|---|
| 64 |
$redirect_url = redirect_guess_404_permalink(); |
|---|
| 65 |
} elseif ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() ) { |
|---|
| 66 |
|
|---|
| 67 |
if ( is_single() && isset($_GET['p']) ) { |
|---|
| 68 |
if ( $redirect_url = get_permalink(get_query_var('p')) ) |
|---|
| 69 |
$redirect['query'] = remove_query_arg('p', $redirect['query']); |
|---|
| 70 |
} elseif ( is_page() && isset($_GET['page_id']) ) { |
|---|
| 71 |
if ( $redirect_url = get_permalink(get_query_var('page_id')) ) |
|---|
| 72 |
$redirect['query'] = remove_query_arg('page_id', $redirect['query']); |
|---|
| 73 |
} elseif ( isset($_GET['m']) && ( is_year() || is_month() || is_day() ) ) { |
|---|
| 74 |
$m = get_query_var('m'); |
|---|
| 75 |
switch ( strlen($m) ) { |
|---|
| 76 |
case 4: |
|---|
| 77 |
$redirect_url = get_year_link($m); |
|---|
| 78 |
break; |
|---|
| 79 |
case 6: |
|---|
| 80 |
$redirect_url = get_month_link( substr($m, 0, 4), substr($m, 4, 2) ); |
|---|
| 81 |
break; |
|---|
| 82 |
case 8: |
|---|
| 83 |
$redirect_url = get_day_link(substr($m, 0, 4), substr($m, 4, 2), substr($m, 6, 2)); |
|---|
| 84 |
break; |
|---|
| 85 |
} |
|---|
| 86 |
if ( $redirect_url ) |
|---|
| 87 |
$redirect['query'] = remove_query_arg('m', $redirect['query']); |
|---|
| 88 |
|
|---|
| 89 |
} elseif ( is_day() && get_query_var('year') && get_query_var('monthnum') && isset($_GET['day']) ) { |
|---|
| 90 |
if ( $redirect_url = get_day_link(get_query_var('year'), get_query_var('monthnum'), get_query_var('day')) ) |
|---|
| 91 |
$redirect['query'] = remove_query_arg(array('year', 'monthnum', 'day'), $redirect['query']); |
|---|
| 92 |
} elseif ( is_month() && get_query_var('year') && isset($_GET['monthnum']) ) { |
|---|
| 93 |
if ( $redirect_url = get_month_link(get_query_var('year'), get_query_var('monthnum')) ) |
|---|
| 94 |
$redirect['query'] = remove_query_arg(array('year', 'monthnum'), $redirect['query']); |
|---|
| 95 |
} elseif ( is_year() && isset($_GET['year']) ) { |
|---|
| 96 |
if ( $redirect_url = get_year_link(get_query_var('year')) ) |
|---|
| 97 |
$redirect['query'] = remove_query_arg('year', $redirect['query']); |
|---|
| 98 |
} elseif ( is_category() && isset($_GET['cat']) ) { |
|---|
| 99 |
if ( $redirect_url = get_category_link(get_query_var('cat')) ) |
|---|
| 100 |
$redirect['query'] = remove_query_arg('cat', $redirect['query']); |
|---|
| 101 |
} elseif ( is_author() && isset($_GET['author']) ) { |
|---|
| 102 |
$author = get_userdata(get_query_var('author')); |
|---|
| 103 |
if ( false !== $author && $redirect_url = get_author_link(false, $author->ID, $author->user_nicename) ) |
|---|
| 104 |
$redirect['query'] = remove_query_arg('author', $redirect['author']); |
|---|
| 105 |
} |
|---|
| 106 |
|
|---|
| 107 |
|
|---|
| 108 |
if ( $paged = get_query_var('paged') ) { |
|---|
| 109 |
if ( $paged > 0 ) { |
|---|
| 110 |
if ( !$redirect_url ) |
|---|
| 111 |
$redirect_url = $requested_url; |
|---|
| 112 |
$paged_redirect = @parse_url($redirect_url); |
|---|
| 113 |
$paged_redirect['path'] = preg_replace('|/page/[0-9]+?(/+)?$|', '/', $paged_redirect['path']); |
|---|
| 114 |
$paged_redirect['path'] = preg_replace('|/index.php/?$|', '/', $paged_redirect['path']); |
|---|
| 115 |
if ( $paged > 1 && !is_single() ) { |
|---|
| 116 |
$paged_redirect['path'] = trailingslashit($paged_redirect['path']); |
|---|
| 117 |
if ( $wp_rewrite->using_index_permalinks() && strpos($paged_redirect['path'], '/index.php/') === false ) |
|---|
| 118 |
$paged_redirect['path'] .= 'index.php/'; |
|---|
| 119 |
$paged_redirect['path'] .= user_trailingslashit("page/$paged", 'paged'); |
|---|
| 120 |
} elseif ( !is_home() && !is_single() ){ |
|---|
| 121 |
$paged_redirect['path'] = user_trailingslashit($paged_redirect['path'], 'paged'); |
|---|
| 122 |
} |
|---|
| 123 |
$redirect_url = $paged_redirect['scheme'] . '://' . $paged_redirect['host'] . $paged_redirect['path']; |
|---|
| 124 |
$redirect['path'] = $paged_redirect['path']; |
|---|
| 125 |
} |
|---|
| 126 |
$redirect['query'] = remove_query_arg('paged', $redirect['query']); |
|---|
| 127 |
} |
|---|
| 128 |
} |
|---|
| 129 |
|
|---|
| 130 |
|
|---|
| 131 |
if ( $redirect_url && $redirect['query'] ) { |
|---|
| 132 |
if ( strpos($redirect_url, '?') !== false ) |
|---|
| 133 |
$redirect_url .= '&'; |
|---|
| 134 |
else |
|---|
| 135 |
$redirect_url .= '?'; |
|---|
| 136 |
$redirect_url .= $redirect['query']; |
|---|
| 137 |
} |
|---|
| 138 |
|
|---|
| 139 |
if ( $redirect_url ) |
|---|
| 140 |
$redirect = @parse_url($redirect_url); |
|---|
| 141 |
|
|---|
| 142 |
|
|---|
| 143 |
$user_home = @parse_url(get_option('home')); |
|---|
| 144 |
if ( isset($user_home['host']) ) |
|---|
| 145 |
$redirect['host'] = $user_home['host']; |
|---|
| 146 |
|
|---|
| 147 |
|
|---|
| 148 |
if ( isset($user_home['port']) ) |
|---|
| 149 |
$redirect['port'] = $user_home['port']; |
|---|
| 150 |
else |
|---|
| 151 |
unset($redirect['port']); |
|---|
| 152 |
|
|---|
| 153 |
|
|---|
| 154 |
$redirect['path'] = preg_replace('|/index.php/$|', '/', $redirect['path']); |
|---|
| 155 |
|
|---|
| 156 |
|
|---|
| 157 |
if ( !$wp_rewrite->using_index_permalinks() ) |
|---|
| 158 |
$redirect['path'] = str_replace('/index.php/', '/', $redirect['path']); |
|---|
| 159 |
|
|---|
| 160 |
|
|---|
| 161 |
if ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() && !is_404() && (!is_home() || ( is_home() && (get_query_var('paged') > 1) ) ) ) { |
|---|
| 162 |
$user_ts_type = ''; |
|---|
| 163 |
if ( get_query_var('paged') > 0 ) { |
|---|
| 164 |
$user_ts_type = 'paged'; |
|---|
| 165 |
} else { |
|---|
| 166 |
foreach ( array('single', 'category', 'page', 'day', 'month', 'year') as $type ) { |
|---|
| 167 |
$func = 'is_' . $type; |
|---|
| 168 |
if ( call_user_func($func) ) |
|---|
| 169 |
$user_ts_type = $type; |
|---|
| 170 |
break; |
|---|
| 171 |
} |
|---|
| 172 |
} |
|---|
| 173 |
$redirect['path'] = user_trailingslashit($redirect['path'], $user_ts_type); |
|---|
| 174 |
} elseif ( is_home() ) { |
|---|
| 175 |
$redirect['path'] = trailingslashit($redirect['path']); |
|---|
| 176 |
} |
|---|
| 177 |
|
|---|
| 178 |
|
|---|
| 179 |
if ( $redirect['path'] == $user_home['path'] ) |
|---|
| 180 |
$redirect['path'] = trailingslashit($redirect['path']); |
|---|
| 181 |
|
|---|
| 182 |
|
|---|
| 183 |
if ( strtolower($original['host']) == strtolower($redirect['host']) ) |
|---|
| 184 |
$redirect['host'] = $original['host']; |
|---|
| 185 |
|
|---|
| 186 |
if ( array($original['host'], $original['port'], $original['path'], $original['query']) !== array($redirect['host'], $redirect['port'], $redirect['path'], $redirect['query']) ) { |
|---|
| 187 |
$redirect_url = $redirect['scheme'] . '://' . $redirect['host']; |
|---|
| 188 |
if ( isset($redirect['port']) ) |
|---|
| 189 |
$redirect_url .= ':' . $redirect['port']; |
|---|
| 190 |
$redirect_url .= $redirect['path']; |
|---|
| 191 |
if ( $redirect['query'] ) |
|---|
| 192 |
$redirect_url .= '?' . $redirect['query']; |
|---|
| 193 |
} |
|---|
| 194 |
|
|---|
| 195 |
if ( !$redirect_url || $redirect_url == $requested_url ) |
|---|
| 196 |
return false; |
|---|
| 197 |
|
|---|
| 198 |
|
|---|
| 199 |
$redirect_url = apply_filters('redirect_canonical', $redirect_url, $requested_url); |
|---|
| 200 |
|
|---|
| 201 |
if ( !$redirect_url || $redirect_url == $requested_url ) |
|---|
| 202 |
return false; |
|---|
| 203 |
|
|---|
| 204 |
if ( $do_redirect ) { |
|---|
| 205 |
|
|---|
| 206 |
if ( !redirect_canonical($redirect_url, false) ) { |
|---|
| 207 |
wp_redirect($redirect_url, 301); |
|---|
| 208 |
exit(); |
|---|
| 209 |
} else { |
|---|
| 210 |
return false; |
|---|
| 211 |
} |
|---|
| 212 |
} else { |
|---|
| 213 |
return $redirect_url; |
|---|
| 214 |
} |
|---|
| 215 |
} |
|---|
| 216 |
|
|---|
| 217 |
|
|---|
| 218 |
|
|---|
| 219 |
|
|---|
| 220 |
|
|---|
| 221 |
|
|---|
| 222 |
|
|---|
| 223 |
|
|---|
| 224 |
|
|---|
| 225 |
|
|---|
| 226 |
function redirect_guess_404_permalink() { |
|---|
| 227 |
global $wpdb; |
|---|
| 228 |
if ( !get_query_var('name') ) |
|---|
| 229 |
return false; |
|---|
| 230 |
|
|---|
| 231 |
$where = $wpdb->prepare("post_name LIKE %s", get_query_var('name') . '%'); |
|---|
| 232 |
|
|---|
| 233 |
|
|---|
| 234 |
if ( get_query_var('year') ) |
|---|
| 235 |
$where .= $wpdb->prepare(" AND YEAR(post_date) = %d", get_query_var('year')); |
|---|
| 236 |
if ( get_query_var('monthnum') ) |
|---|
| 237 |
$where .= $wpdb->prepare(" AND MONTH(post_date) = %d", get_query_var('monthnum')); |
|---|
| 238 |
if ( get_query_var('day') ) |
|---|
| 239 |
$where .= $wpdb->prepare(" AND DAYOFMONTH(post_date) = %d", get_query_var('day')); |
|---|
| 240 |
|
|---|
| 241 |
$post_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE $where AND post_status = 'publish'"); |
|---|
| 242 |
if ( !$post_id ) |
|---|
| 243 |
return false; |
|---|
| 244 |
return get_permalink($post_id); |
|---|
| 245 |
} |
|---|
| 246 |
|
|---|
| 247 |
add_action('template_redirect', 'redirect_canonical'); |
|---|
| 248 |
|
|---|
| 249 |
?> |
|---|
| 250 |
|
|---|