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 | * @package WordPress |
---|
9 | * @since 2.3.0 |
---|
10 | */ |
---|
11 | |
---|
12 | /** |
---|
13 | * Redirects incoming links to the proper URL based on the site url. |
---|
14 | * |
---|
15 | * Search engines consider www.somedomain.com and somedomain.com to be two |
---|
16 | * different URLs when they both go to the same location. This SEO enhancement |
---|
17 | * prevents penality for duplicate content by redirecting all incoming links to |
---|
18 | * one or the other. |
---|
19 | * |
---|
20 | * Prevents redirection for feeds, trackbacks, searches, comment popup, and |
---|
21 | * admin URLs. Does not redirect on IIS, page/post previews, and on form data. |
---|
22 | * |
---|
23 | * Will also attempt to find the correct link when a user enters a URL that does |
---|
24 | * not exist based on exact WordPress query. Will instead try to parse the URL |
---|
25 | * or query in an attempt to figure the correct page to go to. |
---|
26 | * |
---|
27 | * @since 2.3.0 |
---|
28 | * @uses $wp_rewrite |
---|
29 | * @uses $is_IIS |
---|
30 | * |
---|
31 | * @param string $requested_url Optional. The URL that was requested, used to |
---|
32 | * figure if redirect is needed. |
---|
33 | * @param bool $do_redirect Optional. Redirect to the new URL. |
---|
34 | * @return null|false|string Null, if redirect not needed. False, if redirect |
---|
35 | * not needed or the string of the URL |
---|
36 | */ |
---|
37 | function redirect_canonical( $requested_url = null, $do_redirect = true ) { |
---|
38 | global $wp_rewrite, $is_IIS, $wp_query, $wpdb; |
---|
39 | |
---|
40 | if ( is_trackback() || is_search() || is_comments_popup() || is_admin() || !empty($_POST) || is_preview() || is_robots() || $is_IIS ) |
---|
41 | return; |
---|
42 | |
---|
43 | if ( !$requested_url ) { |
---|
44 | // build the URL in the address bar |
---|
45 | $requested_url = is_ssl() ? 'https://' : 'http://'; |
---|
46 | $requested_url .= $_SERVER['HTTP_HOST']; |
---|
47 | $requested_url .= $_SERVER['REQUEST_URI']; |
---|
48 | } |
---|
49 | |
---|
50 | $original = @parse_url($requested_url); |
---|
51 | if ( false === $original ) |
---|
52 | return; |
---|
53 | |
---|
54 | // Some PHP setups turn requests for / into /index.php in REQUEST_URI |
---|
55 | // See: http://trac.wordpress.org/ticket/5017 |
---|
56 | // See: http://trac.wordpress.org/ticket/7173 |
---|
57 | // Disabled, for now: |
---|
58 | // $original['path'] = preg_replace('|/index\.php$|', '/', $original['path']); |
---|
59 | |
---|
60 | $redirect = $original; |
---|
61 | $redirect_url = false; |
---|
62 | |
---|
63 | // Notice fixing |
---|
64 | if ( !isset($redirect['path']) ) |
---|
65 | $redirect['path'] = ''; |
---|
66 | if ( !isset($redirect['query']) ) |
---|
67 | $redirect['query'] = ''; |
---|
68 | |
---|
69 | $qv_remove = array(); |
---|
70 | |
---|
71 | if ( is_singular() && 1 > $wp_query->post_count && ($id = get_query_var('p')) ) { |
---|
72 | |
---|
73 | $vars = $wpdb->get_results( $wpdb->prepare("SELECT post_type, post_parent FROM $wpdb->posts WHERE ID = %d", $id) ); |
---|
74 | |
---|
75 | if ( isset($vars[0]) && $vars = $vars[0] ) { |
---|
76 | if ( 'revision' == $vars->post_type && $vars->post_parent > 0 ) |
---|
77 | $id = $vars->post_parent; |
---|
78 | |
---|
79 | if ( $redirect_url = get_permalink($id) ) |
---|
80 | $qv_remove = array_merge($qv_remove, array('p', 'page_id', 'attachment_id', 'post_type') ); |
---|
81 | } |
---|
82 | } elseif ( is_404() ) { |
---|
83 | // Redirect ?page_id, ?p=, ?attachment_id= to their respective url's |
---|
84 | $id = max( get_query_var('p'), get_query_var('page_id'), get_query_var('attachment_id') ); |
---|
85 | if ( $id && $redirect_post = get_post($id) ) { |
---|
86 | if ( get_post_type_object($redirect_post->post_type)->public ) { |
---|
87 | $redirect_url = get_permalink($redirect_post); |
---|
88 | $qv_remove = array_merge($qv_remove, array('p', 'page_id', 'attachment_id', 'post_type') ); |
---|
89 | } |
---|
90 | } |
---|
91 | |
---|
92 | if ( ! $redirect_url ) { |
---|
93 | $redirect_url = redirect_guess_404_permalink(); |
---|
94 | if ( $redirect_url ) |
---|
95 | $qv_remove = array_merge($qv_remove, array('pagename', 'name') ); |
---|
96 | } |
---|
97 | } elseif ( is_attachment() ) { |
---|
98 | if ( $redirect_url = get_attachment_link(get_query_var('attachment_id')) ) { |
---|
99 | $qv_remove[] = 'attachment_id'; |
---|
100 | $qv_remove[] = 'attachment'; |
---|
101 | } |
---|
102 | } elseif ( is_single() ) { |
---|
103 | if ( $redirect_url = get_permalink( $wp_query->get_queried_object_id() ) ) { |
---|
104 | $qv_remove[] = 'p'; |
---|
105 | $qv_remove[] = 'post_type'; |
---|
106 | $qv_remove[] = 'name'; |
---|
107 | $qv_remove[] = 'pagename'; |
---|
108 | } |
---|
109 | } elseif ( is_page() ) { |
---|
110 | if ( !is_feed() && isset($wp_query->queried_object) && 'page' == get_option('show_on_front') && $wp_query->queried_object->ID == get_option('page_on_front') && ! $redirect_url ) |
---|
111 | $redirect_url = home_url('/'); |
---|
112 | else |
---|
113 | $redirect_url = get_permalink(get_query_var('page_id')); |
---|
114 | if ( $redirect_url ) { |
---|
115 | $qv_remove[] = 'page_id'; |
---|
116 | $qv_remove[] = 'pagename'; |
---|
117 | } |
---|
118 | } elseif ( is_category() || is_tag() || is_tax() ) { // Terms (Tags/categories/Custom Taxonomies) |
---|
119 | |
---|
120 | // @TODO: Buggy, Doesnt account for the fact that categories include child cats in the count. |
---|
121 | $term_count = 0; |
---|
122 | $term_taxonomies = array_unique(wp_list_pluck($wp_query->tax_query->queries, 'taxonomy')); |
---|
123 | foreach ( $wp_query->tax_query->queries as $tax_query ) |
---|
124 | $term_count += count( $tax_query['terms'] ); |
---|
125 | |
---|
126 | $obj = $wp_query->get_queried_object(); |
---|
127 | if ( $term_count <= 1 && !empty($obj->term_id) ) |
---|
128 | $redirect_url = get_term_link((int)$obj->term_id, $obj->taxonomy); |
---|
129 | |
---|
130 | elseif ( $term_count > 1 && 1 == count($term_taxonomies) ) |
---|
131 | $redirect_url = false; // Booyakasha? Build the merged array and win the world? |
---|
132 | |
---|
133 | if ( $term_count == 1 ) { |
---|
134 | // Strip taxonomy query vars off the url. |
---|
135 | $qv_remove[] = 'term'; |
---|
136 | $qv_remove[] = 'taxonomy'; |
---|
137 | } |
---|
138 | |
---|
139 | if ( is_category() ) { |
---|
140 | $qv_remove[] = 'category_name'; |
---|
141 | $qv_remove[] = 'cat'; |
---|
142 | } elseif ( is_tag() ) { |
---|
143 | $qv_remove[] = 'tag'; |
---|
144 | $qv_remove[] = 'tag_id'; |
---|
145 | } else { // Custom taxonomies will have a custom query var, remove those too: |
---|
146 | $tax_obj = get_taxonomy( $obj->taxonomy ); |
---|
147 | if ( false !== $tax_obj->query_var ) |
---|
148 | $qv_remove[] = $tax_obj->query_var; |
---|
149 | } |
---|
150 | } elseif ( is_author() ) { |
---|
151 | $author = get_userdata( get_query_var('author') ); |
---|
152 | if ( false !== $author && $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE $wpdb->posts.post_author = %d AND $wpdb->posts.post_status = 'publish' LIMIT 1", $author->ID ) ) ) { |
---|
153 | if ( $redirect_url = get_author_posts_url($author->ID, $author->user_nicename) ) { |
---|
154 | $qv_remove[] = 'author'; |
---|
155 | $qv_remove[] = 'author_name'; |
---|
156 | } |
---|
157 | } |
---|
158 | } elseif ( is_year() || is_month() || is_day() ) { |
---|
159 | if ( '' != get_query_var('m') ) { |
---|
160 | $m = get_query_var('m'); |
---|
161 | if ( strlen($m) > 6 ) |
---|
162 | $redirect_url = get_day_link(substr($m, 0, 4), substr($m, 4, 2), substr($m, 6, 2)); |
---|
163 | elseif ( strlen($m) > 4 ) |
---|
164 | $redirect_url = get_month_link( substr($m, 0, 4), substr($m, 4, 2) ); |
---|
165 | else |
---|
166 | $redirect_url = get_year_link($m); |
---|
167 | if ( $redirect_url ) |
---|
168 | $qv_remove[] = 'm'; |
---|
169 | } elseif ( is_day() ) { |
---|
170 | if ( get_query_var('year') && get_query_var('monthnum') ) |
---|
171 | $redirect_url = get_day_link(get_query_var('year'), get_query_var('monthnum'), get_query_var('day')); |
---|
172 | $qv_remove = array_merge($qv_remove, array('year', 'monthnum', 'day')); |
---|
173 | } elseif ( is_month() ) { |
---|
174 | if ( get_query_var('year') ) |
---|
175 | if ( $redirect_url = get_month_link(get_query_var('year'), get_query_var('monthnum')) ) |
---|
176 | $qv_remove = array_merge($qv_remove, array('year', 'monthnum')); |
---|
177 | } elseif ( is_year() ) { |
---|
178 | if ( $redirect_url = get_year_link(get_query_var('year')) ) |
---|
179 | $qv_remove[] = 'year'; |
---|
180 | } |
---|
181 | } elseif ( is_single() && strpos($wp_rewrite->permalink_structure, '%category%') !== false ) { |
---|
182 | $category = get_category_by_path(get_query_var('category_name')); |
---|
183 | $post_terms = wp_get_object_terms($wp_query->get_queried_object_id(), 'category', array('fields' => 'tt_ids')); |
---|
184 | if ( (!$category || is_wp_error($category)) || ( !is_wp_error($post_terms) && !empty($post_terms) && !in_array($category->term_taxonomy_id, $post_terms) ) ) |
---|
185 | $redirect_url = get_permalink($wp_query->get_queried_object_id()); |
---|
186 | } elseif ( is_singular() ) { |
---|
187 | $redirect_url = get_permalink( $wp_query->get_queried_object_id() ); |
---|
188 | } else { |
---|
189 | $redirect_url = home_url(); |
---|
190 | } |
---|
191 | |
---|
192 | if ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() ) { |
---|
193 | // Feeds |
---|
194 | if ( is_feed() ) { |
---|
195 | $feed = get_query_var('feed'); |
---|
196 | $redirect_url = trailingslashit( $redirect_url ); |
---|
197 | if ( is_comment_feed() ) { |
---|
198 | $feed = str_replace('comments-', '', $feed); |
---|
199 | $redirect_url .= 'comments/'; |
---|
200 | } |
---|
201 | $redirect_url .= 'feed'; |
---|
202 | if ( $feed != get_default_feed() ) |
---|
203 | $redirect_url .= '/' . $feed; |
---|
204 | $redirect_url = user_trailingslashit( $redirect_url, 'single_single' ); |
---|
205 | $qv_remove[] = 'feed'; |
---|
206 | } |
---|
207 | |
---|
208 | // Comment Paging |
---|
209 | if ( get_query_var('cpage') ) { |
---|
210 | if ( !is_feed() && get_query_var('cpage') > 0 ) |
---|
211 | $redirect_url = trailingslashit($redirect_url) . user_trailingslashit( 'comment-page-' . intval(get_query_var('cpage')), 'commentpaged' ); |
---|
212 | $qv_remove[] = 'cpage'; |
---|
213 | } |
---|
214 | |
---|
215 | // Post Paging |
---|
216 | if ( get_query_var('page') && ( is_singular() || (is_404() && $redirect_url) ) ) { |
---|
217 | $page = get_query_var('page'); |
---|
218 | if ( is_singular() ) { |
---|
219 | setup_postdata(get_queried_object()); |
---|
220 | global $numpages; |
---|
221 | $page = min($page, $numpages); |
---|
222 | } |
---|
223 | if ( !is_feed() ) // Feeds don't support paging |
---|
224 | $redirect_url = trailingslashit( $redirect_url ) . user_trailingslashit( $page, 'single_paged' ); |
---|
225 | $qv_remove[] = 'page'; |
---|
226 | } |
---|
227 | |
---|
228 | // Archive Paging |
---|
229 | if ( get_query_var('paged') > 0 ) { |
---|
230 | if ( !is_feed() ) // Feeds don't support paging |
---|
231 | $redirect_url = trailingslashit( $redirect_url ) . user_trailingslashit( "$wp_rewrite->pagination_base/" . get_query_var( 'paged' ), 'paged' ); |
---|
232 | $qv_remove[] = 'paged'; |
---|
233 | } |
---|
234 | |
---|
235 | if ( !empty($redirect_url) && ( is_singular() || is_404() ) ) { |
---|
236 | // Get the query vars in the current permastruct |
---|
237 | if ( preg_match_all('/(%.+?%)/', $wp_rewrite->permalink_structure, $tokens) ) { |
---|
238 | foreach ( $tokens[0] as $token ) |
---|
239 | $qv_remove[] = trim($wp_rewrite->queryreplace[ array_search($token, $wp_rewrite->rewritecode) ], '='); |
---|
240 | } |
---|
241 | } |
---|
242 | } |
---|
243 | |
---|
244 | /* |
---|
245 | // paging and feeds |
---|
246 | if ( get_query_var('paged') || is_feed() || get_query_var('cpage') ) { |
---|
247 | while ( preg_match( "#/$wp_rewrite->pagination_base/?[0-9]+?(/+)?$#", $redirect['path'] ) || preg_match( '#/(comments/?)?(feed|rss|rdf|atom|rss2)(/+)?$#', $redirect['path'] ) || preg_match( '#/comment-page-[0-9]+(/+)?$#', $redirect['path'] ) ) { |
---|
248 | // Strip off paging and feed |
---|
249 | $redirect['path'] = preg_replace("#/$wp_rewrite->pagination_base/?[0-9]+?(/+)?$#", '/', $redirect['path']); // strip off any existing paging |
---|
250 | $redirect['path'] = preg_replace('#/(comments/?)?(feed|rss2?|rdf|atom)(/+|$)#', '/', $redirect['path']); // strip off feed endings |
---|
251 | $redirect['path'] = preg_replace('#/comment-page-[0-9]+?(/+)?$#', '/', $redirect['path']); // strip off any existing comment paging |
---|
252 | } |
---|
253 | |
---|
254 | $addl_path = ''; |
---|
255 | if ( is_feed() && in_array( get_query_var('feed'), $wp_rewrite->feeds ) ) { |
---|
256 | $addl_path = !empty( $addl_path ) ? trailingslashit($addl_path) : ''; |
---|
257 | if ( get_query_var( 'withcomments' ) ) |
---|
258 | $addl_path .= 'comments/'; |
---|
259 | if ( ( in_array( get_default_feed(), array( 'rss', 'rdf' ) ) && 'feed' == get_query_var('feed') ) || in_array( get_query_var('feed'), array( 'rss', 'rdf' ) ) ) |
---|
260 | $addl_path .= user_trailingslashit( 'feed/' . ( ( get_default_feed() == 'rss2' ) ? '' : 'rss2' ), 'feed' ); |
---|
261 | else |
---|
262 | $addl_path .= user_trailingslashit( 'feed/' . ( ( get_default_feed() == get_query_var('feed') || 'feed' == get_query_var('feed') ) ? '' : get_query_var('feed') ), 'feed' ); |
---|
263 | $redirect['query'] = remove_query_arg( 'feed', $redirect['query'] ); |
---|
264 | } elseif ( is_feed() && 'old' == get_query_var('feed') ) { |
---|
265 | $old_feed_files = array( |
---|
266 | 'wp-atom.php' => 'atom', |
---|
267 | 'wp-commentsrss2.php' => 'comments_rss2', |
---|
268 | 'wp-feed.php' => get_default_feed(), |
---|
269 | 'wp-rdf.php' => 'rss2', |
---|
270 | 'wp-rss.php' => 'rss2', |
---|
271 | 'wp-rss2.php' => 'rss2', |
---|
272 | ); |
---|
273 | if ( isset( $old_feed_files[ basename( $redirect['path'] ) ] ) ) { |
---|
274 | $redirect_url = get_feed_link( $old_feed_files[ basename( $redirect['path'] ) ] ); |
---|
275 | wp_redirect( $redirect_url, 301 ); |
---|
276 | die(); |
---|
277 | } |
---|
278 | } |
---|
279 | |
---|
280 | if ( get_query_var('paged') > 0 ) { |
---|
281 | $paged = get_query_var('paged'); |
---|
282 | $redirect['query'] = remove_query_arg( 'paged', $redirect['query'] ); |
---|
283 | if ( !is_feed() ) { |
---|
284 | if ( $paged > 1 && !is_single() ) { |
---|
285 | $addl_path = ( !empty( $addl_path ) ? trailingslashit($addl_path) : '' ) . user_trailingslashit("$wp_rewrite->pagination_base/$paged", 'paged'); |
---|
286 | } elseif ( !is_single() ) { |
---|
287 | $addl_path = !empty( $addl_path ) ? trailingslashit($addl_path) : ''; |
---|
288 | } |
---|
289 | } elseif ( $paged > 1 ) { |
---|
290 | $redirect['query'] = add_query_arg( 'paged', $paged, $redirect['query'] ); |
---|
291 | } |
---|
292 | } |
---|
293 | |
---|
294 | 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 ) ) ) { |
---|
295 | $addl_path = ( !empty( $addl_path ) ? trailingslashit($addl_path) : '' ) . user_trailingslashit( 'comment-page-' . get_query_var('cpage'), 'commentpaged' ); |
---|
296 | $redirect['query'] = remove_query_arg( 'cpage', $redirect['query'] ); |
---|
297 | } |
---|
298 | |
---|
299 | $redirect['path'] = user_trailingslashit( preg_replace('|/index.php/?$|', '/', $redirect['path']) ); // strip off trailing /index.php/ |
---|
300 | if ( !empty( $addl_path ) && $wp_rewrite->using_index_permalinks() && strpos($redirect['path'], '/index.php/') === false ) |
---|
301 | $redirect['path'] = trailingslashit($redirect['path']) . 'index.php/'; |
---|
302 | if ( !empty( $addl_path ) ) |
---|
303 | $redirect['path'] = trailingslashit($redirect['path']) . $addl_path; |
---|
304 | $redirect_url = $redirect['scheme'] . '://' . $redirect['host'] . $redirect['path']; |
---|
305 | } |
---|
306 | }*/ |
---|
307 | |
---|
308 | if ( empty($redirect_url) ) |
---|
309 | $redirect_url = $requested_url; |
---|
310 | |
---|
311 | $redirect = @parse_url($redirect_url); |
---|
312 | if ( empty($redirect['query']) ) |
---|
313 | $redirect['query'] = ''; |
---|
314 | |
---|
315 | // tack on any additional query vars |
---|
316 | $redirect['query'] = preg_replace( '#^\??&*?#', '', $redirect['query'] ); |
---|
317 | global $wp; |
---|
318 | |
---|
319 | $vars = array_merge($wp->query_vars, $_GET); |
---|
320 | $vars = array_filter($vars); //remove empty |
---|
321 | foreach ( $qv_remove as $qv ) |
---|
322 | unset($vars[ $qv ]); |
---|
323 | |
---|
324 | $redirect['query'] = add_query_arg($vars, $redirect['query']); |
---|
325 | |
---|
326 | // www.example.com vs example.com |
---|
327 | $user_home = @parse_url(home_url()); |
---|
328 | if ( !empty($user_home['host']) ) |
---|
329 | $redirect['host'] = $user_home['host']; |
---|
330 | if ( empty($user_home['path']) ) |
---|
331 | $user_home['path'] = '/'; |
---|
332 | |
---|
333 | // Handle ports |
---|
334 | if ( !empty($user_home['port']) ) |
---|
335 | $redirect['port'] = $user_home['port']; |
---|
336 | else |
---|
337 | unset($redirect['port']); |
---|
338 | |
---|
339 | // trailing /index.php |
---|
340 | $redirect['path'] = preg_replace('|/index.php/*?$|', '/', $redirect['path']); |
---|
341 | |
---|
342 | // Remove trailing spaces from the path |
---|
343 | $redirect['path'] = preg_replace( '#(%20| )+$#', '', $redirect['path'] ); |
---|
344 | |
---|
345 | if ( !empty( $redirect['query'] ) ) { |
---|
346 | // Remove trailing spaces from certain terminating query string args |
---|
347 | $redirect['query'] = preg_replace( '#((p|page_id|cat|tag)=[^&]*?)(%20| )+$#', '$1', $redirect['query'] ); |
---|
348 | |
---|
349 | // Clean up empty query strings |
---|
350 | $redirect['query'] = trim(preg_replace( '#(^|&)(p|page_id|cat|tag)=?(&|$)#', '&', $redirect['query']), '&'); |
---|
351 | |
---|
352 | // Redirect obsolete feeds |
---|
353 | $redirect['query'] = preg_replace( '#(^|&)feed=(rss|rdf)(&|$)#', '$1feed=rss2$3', $redirect['query'] ); |
---|
354 | |
---|
355 | // Remove redundant leading ampersands |
---|
356 | $redirect['query'] = preg_replace( '#^\??&*?#', '', $redirect['query'] ); |
---|
357 | |
---|
358 | // %2c => , |
---|
359 | $redirect['query'] = urldecode($redirect['query']); |
---|
360 | } |
---|
361 | |
---|
362 | // strip /index.php/ when we're not using PATHINFO permalinks |
---|
363 | if ( !$wp_rewrite->using_index_permalinks() ) |
---|
364 | $redirect['path'] = str_replace('/index.php/', '/', $redirect['path']); |
---|
365 | |
---|
366 | // trailing slashes |
---|
367 | if ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() && !is_404() && (!is_front_page() || ( is_front_page() && (get_query_var('paged') > 1) ) ) ) { |
---|
368 | $user_ts_type = ''; |
---|
369 | if ( get_query_var('paged') > 0 ) { |
---|
370 | $user_ts_type = 'paged'; |
---|
371 | } else { |
---|
372 | foreach ( array('single', 'category', 'page', 'day', 'month', 'year', 'home') as $type ) { |
---|
373 | $func = 'is_' . $type; |
---|
374 | if ( call_user_func($func) ) { |
---|
375 | $user_ts_type = $type; |
---|
376 | break; |
---|
377 | } |
---|
378 | } |
---|
379 | } |
---|
380 | $redirect['path'] = user_trailingslashit($redirect['path'], $user_ts_type); |
---|
381 | } elseif ( is_front_page() ) { |
---|
382 | $redirect['path'] = trailingslashit($redirect['path']); |
---|
383 | } |
---|
384 | |
---|
385 | // Strip multiple slashes out of the URL |
---|
386 | if ( strpos($redirect['path'], '//') > -1 ) |
---|
387 | $redirect['path'] = preg_replace('|/+|', '/', $redirect['path']); |
---|
388 | |
---|
389 | // Always trailing slash the Front Page URL |
---|
390 | if ( trailingslashit( $redirect['path'] ) == trailingslashit( $user_home['path'] ) ) |
---|
391 | $redirect['path'] = trailingslashit($redirect['path']); |
---|
392 | /* |
---|
393 | // Ignore differences in host capitalization, as this can lead to infinite redirects |
---|
394 | // Only redirect no-www <=> yes-www |
---|
395 | if ( strtolower($original['host']) == strtolower($redirect['host']) || |
---|
396 | ( strtolower($original['host']) != 'www.' . strtolower($redirect['host']) && 'www.' . strtolower($original['host']) != strtolower($redirect['host']) ) ) |
---|
397 | $redirect['host'] = $original['host']; |
---|
398 | |
---|
399 | $compare_original = array($original['host'], $original['path']); |
---|
400 | |
---|
401 | if ( !empty( $original['port'] ) ) |
---|
402 | $compare_original[] = $original['port']; |
---|
403 | |
---|
404 | if ( !empty( $original['query'] ) ) |
---|
405 | $compare_original[] = $original['query']; |
---|
406 | |
---|
407 | $compare_redirect = array($redirect['host'], $redirect['path']); |
---|
408 | |
---|
409 | if ( !empty( $redirect['port'] ) ) |
---|
410 | $compare_redirect[] = $redirect['port']; |
---|
411 | |
---|
412 | if ( !empty( $redirect['query'] ) ) |
---|
413 | $compare_redirect[] = $redirect['query']; |
---|
414 | */ |
---|
415 | // if ( $compare_original !== $compare_redirect ) { |
---|
416 | |
---|
417 | $redirect_url = $redirect['scheme'] . '://' . $redirect['host']; |
---|
418 | if ( !empty($redirect['port']) ) |
---|
419 | $redirect_url .= ':' . $redirect['port']; |
---|
420 | $redirect_url .= $redirect['path']; |
---|
421 | if ( !empty($redirect['query']) ) |
---|
422 | $redirect_url .= '?' . $redirect['query']; |
---|
423 | // } |
---|
424 | |
---|
425 | // if ( !$redirect_url || $redirect_url == $requested_url ) |
---|
426 | // return false; |
---|
427 | |
---|
428 | // Hex encoded octets are case-insensitive. |
---|
429 | if ( false !== strpos($requested_url, '%') ) { |
---|
430 | if ( !function_exists('lowercase_octets') ) { |
---|
431 | function lowercase_octets($matches) { |
---|
432 | return strtolower( $matches[0] ); |
---|
433 | } |
---|
434 | } |
---|
435 | $requested_url = preg_replace_callback('|%[a-fA-F0-9][a-fA-F0-9]|', 'lowercase_octets', $requested_url); |
---|
436 | } |
---|
437 | |
---|
438 | // Note that you can use the "redirect_canonical" filter to cancel a canonical redirect for whatever reason by returning FALSE |
---|
439 | $redirect_url = apply_filters('redirect_canonical', $redirect_url, $requested_url); |
---|
440 | |
---|
441 | if ( !$redirect_url || $redirect_url == $requested_url ) // yes, again -- in case the filter aborted the request |
---|
442 | return false; |
---|
443 | |
---|
444 | if ( $do_redirect ) { |
---|
445 | // protect against chained redirects |
---|
446 | if ( !redirect_canonical($redirect_url, false) ) { |
---|
447 | wp_redirect($redirect_url, 301); |
---|
448 | exit(); |
---|
449 | } else { |
---|
450 | // Debug |
---|
451 | // die("1: $redirect_url<br />2: " . redirect_canonical( $redirect_url, false ) ); |
---|
452 | return false; |
---|
453 | } |
---|
454 | } else { |
---|
455 | return $redirect_url; |
---|
456 | } |
---|
457 | } |
---|
458 | |
---|
459 | /** |
---|
460 | * Attempts to guess correct post based on query vars. |
---|
461 | * |
---|
462 | * @since 2.3.0 |
---|
463 | * @uses $wpdb |
---|
464 | * |
---|
465 | * @return bool|string Returns False, if it can't find post, returns correct |
---|
466 | * location on success. |
---|
467 | */ |
---|
468 | function redirect_guess_404_permalink() { |
---|
469 | global $wpdb; |
---|
470 | |
---|
471 | if ( !get_query_var('name') ) |
---|
472 | return false; |
---|
473 | |
---|
474 | $where = $wpdb->prepare("post_name LIKE %s", like_escape( get_query_var('name') ) . '%'); |
---|
475 | |
---|
476 | // if any of post_type, year, monthnum, or day are set, use them to refine the query |
---|
477 | if ( get_query_var('post_type') ) |
---|
478 | $where .= $wpdb->prepare(" AND post_type = %s", get_query_var('post_type')); |
---|
479 | if ( get_query_var('year') ) |
---|
480 | $where .= $wpdb->prepare(" AND YEAR(post_date) = %d", get_query_var('year')); |
---|
481 | if ( get_query_var('monthnum') ) |
---|
482 | $where .= $wpdb->prepare(" AND MONTH(post_date) = %d", get_query_var('monthnum')); |
---|
483 | if ( get_query_var('day') ) |
---|
484 | $where .= $wpdb->prepare(" AND DAYOFMONTH(post_date) = %d", get_query_var('day')); |
---|
485 | |
---|
486 | $post_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE $where AND post_status = 'publish'"); |
---|
487 | if ( !$post_id ) |
---|
488 | return false; |
---|
489 | return get_permalink($post_id); |
---|
490 | } |
---|
491 | |
---|
492 | add_action('template_redirect', 'redirect_canonical'); |
---|
493 | |
---|
494 | ?> |
---|