136
|
1 |
<?php |
|
2 |
/** |
|
3 |
* Canonical API to handle WordPress Redirecting |
|
4 |
* |
|
5 |
* Based on "Permalink Redirect" from Scott Yang and "Enforce www. Preference" |
|
6 |
* by Mark Jaquith |
|
7 |
* |
|
8 |
* @author Scott Yang |
|
9 |
* @author Mark Jaquith |
|
10 |
* @package WordPress |
|
11 |
* @since 2.3.0 |
|
12 |
*/ |
|
13 |
|
|
14 |
/** |
|
15 |
* Redirects incoming links to the proper URL based on the site url. |
|
16 |
* |
|
17 |
* Search engines consider www.somedomain.com and somedomain.com to be two |
|
18 |
* different URLs when they both go to the same location. This SEO enhancement |
|
19 |
* prevents penality for duplicate content by redirecting all incoming links to |
|
20 |
* one or the other. |
|
21 |
* |
|
22 |
* Prevents redirection for feeds, trackbacks, searches, comment popup, and |
|
23 |
* admin URLs. Does not redirect on IIS, page/post previews, and on form data. |
|
24 |
* |
|
25 |
* Will also attempt to find the correct link when a user enters a URL that does |
|
26 |
* not exist based on exact WordPress query. Will instead try to parse the URL |
|
27 |
* or query in an attempt to figure the correct page to go to. |
|
28 |
* |
|
29 |
* @since 2.3.0 |
|
30 |
* @uses $wp_rewrite |
|
31 |
* @uses $is_IIS |
|
32 |
* |
|
33 |
* @param string $requested_url Optional. The URL that was requested, used to |
|
34 |
* figure if redirect is needed. |
|
35 |
* @param bool $do_redirect Optional. Redirect to the new URL. |
|
36 |
* @return null|false|string Null, if redirect not needed. False, if redirect |
|
37 |
* not needed or the string of the URL |
|
38 |
*/ |
|
39 |
function redirect_canonical($requested_url=null, $do_redirect=true) { |
|
40 |
global $wp_rewrite, $is_IIS, $wp_query, $wpdb; |
|
41 |
|
|
42 |
if ( 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 |
// build the URL in the address bar |
|
47 |
$requested_url = ( !empty($_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 |
// Some PHP setups turn requests for / into /index.php in REQUEST_URI |
|
57 |
// See: http://trac.wordpress.org/ticket/5017 |
|
58 |
// See: http://trac.wordpress.org/ticket/7173 |
|
59 |
// Disabled, for now: |
|
60 |
// $original['path'] = preg_replace('|/index\.php$|', '/', $original['path']); |
|
61 |
|
|
62 |
$redirect = $original; |
|
63 |
$redirect_url = false; |
|
64 |
|
|
65 |
// Notice fixing |
|
66 |
if ( !isset($redirect['path']) ) $redirect['path'] = ''; |
|
67 |
if ( !isset($redirect['query']) ) $redirect['query'] = ''; |
|
68 |
|
|
69 |
if ( is_singular() && 1 > $wp_query->post_count && ($id = get_query_var('p')) ) { |
|
70 |
|
|
71 |
$vars = $wpdb->get_results( $wpdb->prepare("SELECT post_type, post_parent FROM $wpdb->posts WHERE ID = %d", $id) ); |
|
72 |
|
|
73 |
if ( isset($vars[0]) && $vars = $vars[0] ) { |
|
74 |
if ( 'revision' == $vars->post_type && $vars->post_parent > 0 ) |
|
75 |
$id = $vars->post_parent; |
|
76 |
|
|
77 |
if ( $redirect_url = get_permalink($id) ) |
|
78 |
$redirect['query'] = remove_query_arg(array('p', 'page_id', 'attachment_id'), $redirect['query']); |
|
79 |
} |
|
80 |
} |
|
81 |
|
|
82 |
// These tests give us a WP-generated permalink |
|
83 |
if ( is_404() ) { |
|
84 |
$redirect_url = redirect_guess_404_permalink(); |
|
85 |
} elseif ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() ) { |
|
86 |
// rewriting of old ?p=X, ?m=2004, ?m=200401, ?m=20040101 |
|
87 |
if ( is_attachment() && !empty($_GET['attachment_id']) && ! $redirect_url ) { |
|
88 |
if ( $redirect_url = get_attachment_link(get_query_var('attachment_id')) ) |
|
89 |
$redirect['query'] = remove_query_arg('attachment_id', $redirect['query']); |
|
90 |
} elseif ( is_single() && !empty($_GET['p']) && ! $redirect_url ) { |
|
91 |
if ( $redirect_url = get_permalink(get_query_var('p')) ) |
|
92 |
$redirect['query'] = remove_query_arg('p', $redirect['query']); |
|
93 |
if ( get_query_var( 'page' ) ) { |
|
94 |
$redirect_url = trailingslashit( $redirect_url ) . user_trailingslashit( get_query_var( 'page' ), 'single_paged' ); |
|
95 |
$redirect['query'] = remove_query_arg( 'page', $redirect['query'] ); |
|
96 |
} |
|
97 |
} elseif ( is_single() && !empty($_GET['name']) && ! $redirect_url ) { |
|
98 |
if ( $redirect_url = get_permalink( $wp_query->get_queried_object_id() ) ) |
|
99 |
$redirect['query'] = remove_query_arg('name', $redirect['query']); |
|
100 |
} elseif ( is_page() && !empty($_GET['page_id']) && ! $redirect_url ) { |
|
101 |
if ( $redirect_url = get_permalink(get_query_var('page_id')) ) |
|
102 |
$redirect['query'] = remove_query_arg('page_id', $redirect['query']); |
|
103 |
} elseif ( !empty($_GET['m']) && ( is_year() || is_month() || is_day() ) ) { |
|
104 |
$m = get_query_var('m'); |
|
105 |
switch ( strlen($m) ) { |
|
106 |
case 4: // Yearly |
|
107 |
$redirect_url = get_year_link($m); |
|
108 |
break; |
|
109 |
case 6: // Monthly |
|
110 |
$redirect_url = get_month_link( substr($m, 0, 4), substr($m, 4, 2) ); |
|
111 |
break; |
|
112 |
case 8: // Daily |
|
113 |
$redirect_url = get_day_link(substr($m, 0, 4), substr($m, 4, 2), substr($m, 6, 2)); |
|
114 |
break; |
|
115 |
} |
|
116 |
if ( $redirect_url ) |
|
117 |
$redirect['query'] = remove_query_arg('m', $redirect['query']); |
|
118 |
// now moving on to non ?m=X year/month/day links |
|
119 |
} elseif ( is_day() && get_query_var('year') && get_query_var('monthnum') && !empty($_GET['day']) ) { |
|
120 |
if ( $redirect_url = get_day_link(get_query_var('year'), get_query_var('monthnum'), get_query_var('day')) ) |
|
121 |
$redirect['query'] = remove_query_arg(array('year', 'monthnum', 'day'), $redirect['query']); |
|
122 |
} elseif ( is_month() && get_query_var('year') && !empty($_GET['monthnum']) ) { |
|
123 |
if ( $redirect_url = get_month_link(get_query_var('year'), get_query_var('monthnum')) ) |
|
124 |
$redirect['query'] = remove_query_arg(array('year', 'monthnum'), $redirect['query']); |
|
125 |
} elseif ( is_year() && !empty($_GET['year']) ) { |
|
126 |
if ( $redirect_url = get_year_link(get_query_var('year')) ) |
|
127 |
$redirect['query'] = remove_query_arg('year', $redirect['query']); |
|
128 |
} elseif ( is_category() && !empty($_GET['cat']) && preg_match( '|^[0-9]+$|', $_GET['cat'] ) ) { |
|
129 |
if ( $redirect_url = get_category_link(get_query_var('cat')) ) |
|
130 |
$redirect['query'] = remove_query_arg('cat', $redirect['query']); |
|
131 |
} elseif ( is_author() && !empty($_GET['author']) && preg_match( '|^[0-9]+$|', $_GET['author'] ) ) { |
|
132 |
$author = get_userdata(get_query_var('author')); |
|
133 |
if ( false !== $author && $redirect_url = get_author_posts_url($author->ID, $author->user_nicename) ) |
|
134 |
$redirect['query'] = remove_query_arg('author', $redirect['author']); |
|
135 |
} |
|
136 |
|
|
137 |
// paging and feeds |
|
138 |
if ( get_query_var('paged') || is_feed() || get_query_var('cpage') ) { |
|
139 |
if ( !$redirect_url ) |
|
140 |
$redirect_url = $requested_url; |
|
141 |
$paged_redirect = @parse_url($redirect_url); |
|
142 |
while ( preg_match( '#/page/[0-9]+?(/+)?$#', $paged_redirect['path'] ) || preg_match( '#/(comments/?)?(feed|rss|rdf|atom|rss2)(/+)?$#', $paged_redirect['path'] ) || preg_match( '#/comment-page-[0-9]+(/+)?$#', $paged_redirect['path'] ) ) { |
|
143 |
// Strip off paging and feed |
|
144 |
$paged_redirect['path'] = preg_replace('#/page/[0-9]+?(/+)?$#', '/', $paged_redirect['path']); // strip off any existing paging |
|
145 |
$paged_redirect['path'] = preg_replace('#/(comments/?)?(feed|rss2?|rdf|atom)(/+|$)#', '/', $paged_redirect['path']); // strip off feed endings |
|
146 |
$paged_redirect['path'] = preg_replace('#/comment-page-[0-9]+?(/+)?$#', '/', $paged_redirect['path']); // strip off any existing comment paging |
|
147 |
} |
|
148 |
|
|
149 |
$addl_path = ''; |
|
150 |
if ( is_feed() ) { |
|
151 |
$addl_path = !empty( $addl_path ) ? trailingslashit($addl_path) : ''; |
|
152 |
if ( get_query_var( 'withcomments' ) ) |
|
153 |
$addl_path .= 'comments/'; |
|
154 |
$addl_path .= user_trailingslashit( 'feed/' . ( ( 'rss2' == get_query_var('feed') || 'feed' == get_query_var('feed') ) ? '' : get_query_var('feed') ), 'feed' ); |
|
155 |
$redirect['query'] = remove_query_arg( 'feed', $redirect['query'] ); |
|
156 |
} |
|
157 |
|
|
158 |
if ( get_query_var('paged') > 0 ) { |
|
159 |
$paged = get_query_var('paged'); |
|
160 |
$redirect['query'] = remove_query_arg( 'paged', $redirect['query'] ); |
|
161 |
if ( !is_feed() ) { |
|
162 |
if ( $paged > 1 && !is_single() ) { |
|
163 |
$addl_path = ( !empty( $addl_path ) ? trailingslashit($addl_path) : '' ) . user_trailingslashit("page/$paged", 'paged'); |
|
164 |
} elseif ( !is_single() ) { |
|
165 |
$addl_path = ( !empty( $addl_path ) ? trailingslashit($addl_path) : '' ) . user_trailingslashit($paged_redirect['path'], 'paged'); |
|
166 |
} |
|
167 |
} elseif ( $paged > 1 ) { |
|
168 |
$redirect['query'] = add_query_arg( 'paged', $paged, $redirect['query'] ); |
|
169 |
} |
|
170 |
} |
|
171 |
|
|
172 |
if ( get_option('page_comments') && ( ( 'newest' == get_option('default_comments_page') && get_query_var('cpage') > 0 ) || ( 'newest' != get_option('default_comments_page') && get_query_var('cpage') > 1 ) ) ) { |
|
173 |
$addl_path = ( !empty( $addl_path ) ? trailingslashit($addl_path) : '' ) . user_trailingslashit( 'comment-page-' . get_query_var('cpage'), 'commentpaged' ); |
|
174 |
$redirect['query'] = remove_query_arg( 'cpage', $redirect['query'] ); |
|
175 |
} |
|
176 |
|
|
177 |
$paged_redirect['path'] = user_trailingslashit( preg_replace('|/index.php/?$|', '/', $paged_redirect['path']) ); // strip off trailing /index.php/ |
|
178 |
if ( !empty( $addl_path ) && $wp_rewrite->using_index_permalinks() && strpos($paged_redirect['path'], '/index.php/') === false ) |
|
179 |
$paged_redirect['path'] = trailingslashit($paged_redirect['path']) . 'index.php/'; |
|
180 |
if ( !empty( $addl_path ) ) |
|
181 |
$paged_redirect['path'] = trailingslashit($paged_redirect['path']) . $addl_path; |
|
182 |
$redirect_url = $paged_redirect['scheme'] . '://' . $paged_redirect['host'] . $paged_redirect['path']; |
|
183 |
$redirect['path'] = $paged_redirect['path']; |
|
184 |
} |
|
185 |
} |
|
186 |
|
|
187 |
// tack on any additional query vars |
|
188 |
$redirect['query'] = preg_replace( '#^\??&*?#', '', $redirect['query'] ); |
|
189 |
if ( $redirect_url && !empty($redirect['query']) ) { |
|
190 |
if ( strpos($redirect_url, '?') !== false ) |
|
191 |
$redirect_url .= '&'; |
|
192 |
else |
|
193 |
$redirect_url .= '?'; |
|
194 |
$redirect_url .= $redirect['query']; |
|
195 |
} |
|
196 |
|
|
197 |
if ( $redirect_url ) |
|
198 |
$redirect = @parse_url($redirect_url); |
|
199 |
|
|
200 |
// www.example.com vs example.com |
|
201 |
$user_home = @parse_url(get_option('home')); |
|
202 |
if ( !empty($user_home['host']) ) |
|
203 |
$redirect['host'] = $user_home['host']; |
|
204 |
if ( empty($user_home['path']) ) |
|
205 |
$user_home['path'] = '/'; |
|
206 |
|
|
207 |
// Handle ports |
|
208 |
if ( !empty($user_home['port']) ) |
|
209 |
$redirect['port'] = $user_home['port']; |
|
210 |
else |
|
211 |
unset($redirect['port']); |
|
212 |
|
|
213 |
// trailing /index.php |
|
214 |
$redirect['path'] = preg_replace('|/index.php/*?$|', '/', $redirect['path']); |
|
215 |
|
|
216 |
// Remove trailing spaces from the path |
|
217 |
$redirect['path'] = preg_replace( '#(%20| )+$#', '', $redirect['path'] ); |
|
218 |
|
|
219 |
if ( !empty( $redirect['query'] ) ) { |
|
220 |
// Remove trailing spaces from certain terminating query string args |
|
221 |
$redirect['query'] = preg_replace( '#((p|page_id|cat|tag)=[^&]*?)(%20| )+$#', '$1', $redirect['query'] ); |
|
222 |
|
|
223 |
// Clean up empty query strings |
|
224 |
$redirect['query'] = trim(preg_replace( '#(^|&)(p|page_id|cat|tag)=?(&|$)#', '&', $redirect['query']), '&'); |
|
225 |
|
|
226 |
// Remove redundant leading ampersands |
|
227 |
$redirect['query'] = preg_replace( '#^\??&*?#', '', $redirect['query'] ); |
|
228 |
} |
|
229 |
|
|
230 |
// strip /index.php/ when we're not using PATHINFO permalinks |
|
231 |
if ( !$wp_rewrite->using_index_permalinks() ) |
|
232 |
$redirect['path'] = str_replace('/index.php/', '/', $redirect['path']); |
|
233 |
|
|
234 |
// trailing slashes |
|
235 |
if ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() && !is_404() && (!is_front_page() || ( is_front_page() && (get_query_var('paged') > 1) ) ) ) { |
|
236 |
$user_ts_type = ''; |
|
237 |
if ( get_query_var('paged') > 0 ) { |
|
238 |
$user_ts_type = 'paged'; |
|
239 |
} else { |
|
240 |
foreach ( array('single', 'category', 'page', 'day', 'month', 'year', 'home') as $type ) { |
|
241 |
$func = 'is_' . $type; |
|
242 |
if ( call_user_func($func) ) { |
|
243 |
$user_ts_type = $type; |
|
244 |
break; |
|
245 |
} |
|
246 |
} |
|
247 |
} |
|
248 |
$redirect['path'] = user_trailingslashit($redirect['path'], $user_ts_type); |
|
249 |
} elseif ( is_front_page() ) { |
|
250 |
$redirect['path'] = trailingslashit($redirect['path']); |
|
251 |
} |
|
252 |
|
|
253 |
// Always trailing slash the Front Page URL |
|
254 |
if ( trailingslashit( $redirect['path'] ) == trailingslashit( $user_home['path'] ) ) |
|
255 |
$redirect['path'] = trailingslashit($redirect['path']); |
|
256 |
|
|
257 |
// Ignore differences in host capitalization, as this can lead to infinite redirects |
|
258 |
// Only redirect no-www <=> yes-www |
|
259 |
if ( strtolower($original['host']) == strtolower($redirect['host']) || |
|
260 |
( strtolower($original['host']) != 'www.' . strtolower($redirect['host']) && 'www.' . strtolower($original['host']) != strtolower($redirect['host']) ) ) |
|
261 |
$redirect['host'] = $original['host']; |
|
262 |
|
|
263 |
$compare_original = array($original['host'], $original['path']); |
|
264 |
|
|
265 |
if ( !empty( $original['port'] ) ) |
|
266 |
$compare_original[] = $original['port']; |
|
267 |
|
|
268 |
if ( !empty( $original['query'] ) ) |
|
269 |
$compare_original[] = $original['query']; |
|
270 |
|
|
271 |
$compare_redirect = array($redirect['host'], $redirect['path']); |
|
272 |
|
|
273 |
if ( !empty( $redirect['port'] ) ) |
|
274 |
$compare_redirect[] = $redirect['port']; |
|
275 |
|
|
276 |
if ( !empty( $redirect['query'] ) ) |
|
277 |
$compare_redirect[] = $redirect['query']; |
|
278 |
|
|
279 |
if ( $compare_original !== $compare_redirect ) { |
|
280 |
$redirect_url = $redirect['scheme'] . '://' . $redirect['host']; |
|
281 |
if ( !empty($redirect['port']) ) |
|
282 |
$redirect_url .= ':' . $redirect['port']; |
|
283 |
$redirect_url .= $redirect['path']; |
|
284 |
if ( !empty($redirect['query']) ) |
|
285 |
$redirect_url .= '?' . $redirect['query']; |
|
286 |
} |
|
287 |
|
|
288 |
if ( $redirect_url == $requested_url ) |
|
289 |
return false; |
|
290 |
|
|
291 |
// Note that you can use the "redirect_canonical" filter to cancel a canonical redirect for whatever reason by returning FALSE |
|
292 |
$redirect_url = apply_filters('redirect_canonical', $redirect_url, $requested_url); |
|
293 |
|
|
294 |
if ( !$redirect_url || $redirect_url == $requested_url ) // yes, again -- in case the filter aborted the request |
|
295 |
return false; |
|
296 |
|
|
297 |
if ( $do_redirect ) { |
|
298 |
// protect against chained redirects |
|
299 |
if ( !redirect_canonical($redirect_url, false) ) { |
|
300 |
wp_redirect($redirect_url, 301); |
|
301 |
exit(); |
|
302 |
} else { |
|
303 |
// Debug |
|
304 |
// die("1: $redirect_url<br />2: " . redirect_canonical( $redirect_url, false ) ); |
|
305 |
return false; |
|
306 |
} |
|
307 |
} else { |
|
308 |
return $redirect_url; |
|
309 |
} |
|
310 |
} |
|
311 |
|
|
312 |
/** |
|
313 |
* Attempts to guess correct post based on query vars. |
|
314 |
* |
|
315 |
* @since 2.3.0 |
|
316 |
* @uses $wpdb |
|
317 |
* |
|
318 |
* @return bool|string Returns False, if it can't find post, returns correct |
|
319 |
* location on success. |
|
320 |
*/ |
|
321 |
function redirect_guess_404_permalink() { |
|
322 |
global $wpdb; |
|
323 |
|
|
324 |
if ( !get_query_var('name') ) |
|
325 |
return false; |
|
326 |
|
|
327 |
$where = $wpdb->prepare("post_name LIKE %s", get_query_var('name') . '%'); |
|
328 |
|
|
329 |
// if any of year, monthnum, or day are set, use them to refine the query |
|
330 |
if ( get_query_var('year') ) |
|
331 |
$where .= $wpdb->prepare(" AND YEAR(post_date) = %d", get_query_var('year')); |
|
332 |
if ( get_query_var('monthnum') ) |
|
333 |
$where .= $wpdb->prepare(" AND MONTH(post_date) = %d", get_query_var('monthnum')); |
|
334 |
if ( get_query_var('day') ) |
|
335 |
$where .= $wpdb->prepare(" AND DAYOFMONTH(post_date) = %d", get_query_var('day')); |
|
336 |
|
|
337 |
$post_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE $where AND post_status = 'publish'"); |
|
338 |
if ( !$post_id ) |
|
339 |
return false; |
|
340 |
return get_permalink($post_id); |
|
341 |
} |
|
342 |
|
|
343 |
add_action('template_redirect', 'redirect_canonical'); |
|
344 |
|
|
345 |
?> |