]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/canonical.php
Wordpress 2.6.2-scripts
[autoinstalls/wordpress.git] / wp-includes / canonical.php
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
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
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;
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                 // build the URL in the address bar
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         // Some PHP setups turn requests for / into /index.php in REQUEST_URI
57         $original['path'] = preg_replace('|/index\.php$|', '/', $original['path']);
58
59         $redirect = $original;
60         $redirect_url = false;
61
62         // These tests give us a WP-generated permalink
63         if ( is_404() ) {
64                 $redirect_url = redirect_guess_404_permalink();
65         } elseif ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() ) {
66                 // rewriting of old ?p=X, ?m=2004, ?m=200401, ?m=20040101
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: // Yearly
77                                         $redirect_url = get_year_link($m);
78                                         break;
79                                 case 6: // Monthly
80                                         $redirect_url = get_month_link( substr($m, 0, 4), substr($m, 4, 2) );
81                                         break;
82                                 case 8: // Daily
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                 // now moving on to non ?m=X year/month/day links
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         // paging
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']); // strip off any existing paging
114                                 $paged_redirect['path'] = preg_replace('|/index.php/?$|', '/', $paged_redirect['path']); // strip off trailing /index.php/
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         // tack on any additional query vars
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         // www.example.com vs example.com
143         $user_home = @parse_url(get_option('home'));
144         if ( isset($user_home['host']) )
145                 $redirect['host'] = $user_home['host'];
146
147         // Handle ports
148         if ( isset($user_home['port']) )
149                 $redirect['port'] = $user_home['port'];
150         else
151                 unset($redirect['port']);
152
153         // trailing /index.php/
154         $redirect['path'] = preg_replace('|/index.php/$|', '/', $redirect['path']);
155
156         // strip /index.php/ when we're not using PATHINFO permalinks
157         if ( !$wp_rewrite->using_index_permalinks() )
158                 $redirect['path'] = str_replace('/index.php/', '/', $redirect['path']);
159
160         // trailing slashes
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                 }
174                 $redirect['path'] = user_trailingslashit($redirect['path'], $user_ts_type);
175         } elseif ( is_home() ) {
176                 $redirect['path'] = trailingslashit($redirect['path']);
177         }
178
179         // Always trailing slash the 'home' URL
180         if ( $redirect['path'] == $user_home['path'] )
181                 $redirect['path'] = trailingslashit($redirect['path']);
182
183         // Ignore differences in host capitalization, as this can lead to infinite redirects
184         if ( strtolower($original['host']) == strtolower($redirect['host']) )
185                 $redirect['host'] = $original['host'];
186
187         if ( array($original['host'], $original['port'], $original['path'], $original['query']) !== array($redirect['host'], $redirect['port'], $redirect['path'], $redirect['query']) ) {
188                 $redirect_url = $redirect['scheme'] . '://' . $redirect['host'];
189                 if ( isset($redirect['port']) )
190                         $redirect_url .= ':' . $redirect['port'];
191                 $redirect_url .= $redirect['path'];
192                 if ( $redirect['query'] )
193                         $redirect_url .= '?' . $redirect['query'];
194         }
195
196         if ( !$redirect_url || $redirect_url == $requested_url )
197                 return false;
198
199         // Note that you can use the "redirect_canonical" filter to cancel a canonical redirect for whatever reason by returning FALSE
200         $redirect_url = apply_filters('redirect_canonical', $redirect_url, $requested_url);
201
202         if ( !$redirect_url || $redirect_url == $requested_url ) // yes, again -- in case the filter aborted the request
203                 return false;
204
205         if ( $do_redirect ) {
206                 // protect against chained redirects
207                 if ( !redirect_canonical($redirect_url, false) ) {
208                         wp_redirect($redirect_url, 301);
209                         exit();
210                 } else {
211                         return false;
212                 }
213         } else {
214                 return $redirect_url;
215         }
216 }
217
218 /**
219  * Attempts to guess correct post based on query vars
220  *
221  * @since 2.3
222  * @uses $wpdb
223  *
224  * @return bool|string Returns False, if it can't find post, returns correct
225  *              location on success.
226  */
227 function redirect_guess_404_permalink() {
228         global $wpdb;
229         if ( !get_query_var('name') )
230                 return false;
231
232         $where = $wpdb->prepare("post_name LIKE %s", get_query_var('name') . '%');
233
234         // if any of year, monthnum, or day are set, use them to refine the query
235         if ( get_query_var('year') )
236                 $where .= $wpdb->prepare(" AND YEAR(post_date) = %d", get_query_var('year'));
237         if ( get_query_var('monthnum') )
238                 $where .= $wpdb->prepare(" AND MONTH(post_date) = %d", get_query_var('monthnum'));
239         if ( get_query_var('day') )
240                 $where .= $wpdb->prepare(" AND DAYOFMONTH(post_date) = %d", get_query_var('day'));
241
242         $post_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE $where AND post_status = 'publish'");
243         if ( !$post_id )
244                 return false;
245         return get_permalink($post_id);
246 }
247
248 add_action('template_redirect', 'redirect_canonical');
249
250 ?>