]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/canonical.php
Wordpress 2.5.1-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" by Mark Jaquith
6  *
7  * @author Scott Yang
8  * @author Mark Jaquith
9  * @package WordPress
10  * @since 2.3
11  */
12
13 /**
14  * redirect_canonical() - Redirects incoming links to the proper URL based on the site url
15  *
16  * Search engines consider www.somedomain.com and somedomain.com to be two different URLs
17  * when they both go to the same location. This SEO enhancement prevents penality for
18  * duplicate content by redirecting all incoming links to one or the other.
19  *
20  * Prevents redirection for feeds, trackbacks, searches, comment popup, and admin URLs.
21  * 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 not exist
24  * based on exact WordPress query. Will instead try to parse the URL or query in an attempt
25  * to figure the correct page to go to.
26  *
27  * @since 2.3
28  * @uses $wp_rewrite
29  * @uses $is_IIS
30  *
31  * @param string $requested_url Optional. The URL that was requested, used to figure if redirect is needed.
32  * @param bool $do_redirect Optional. Redirect to the new URL.
33  * @return null|false|string Null, if redirect not needed. False, if redirect not needed or the string of the URL
34  */
35 function redirect_canonical($requested_url=null, $do_redirect=true) {
36         global $wp_rewrite, $is_IIS;
37
38         if ( is_feed() || is_trackback() || is_search() || is_comments_popup() || is_admin() || $is_IIS || ( isset($_POST) && count($_POST) ) || is_preview() )
39                 return;
40
41         if ( !$requested_url ) {
42                 // build the URL in the address bar
43                 $requested_url  = ( isset($_SERVER['HTTPS'] ) && strtolower($_SERVER['HTTPS']) == 'on' ) ? 'https://' : 'http://';
44                 $requested_url .= $_SERVER['HTTP_HOST'];
45                 $requested_url .= $_SERVER['REQUEST_URI'];
46         }
47
48         $original = @parse_url($requested_url);
49         if ( false === $original )
50                 return;
51
52         // Some PHP setups turn requests for / into /index.php in REQUEST_URI
53         $original['path'] = preg_replace('|/index\.php$|', '/', $original['path']);
54
55         $redirect = $original;
56         $redirect_url = false;
57
58         // These tests give us a WP-generated permalink
59         if ( is_404() ) {
60                 $redirect_url = redirect_guess_404_permalink();
61         } elseif ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() ) {
62                 // rewriting of old ?p=X, ?m=2004, ?m=200401, ?m=20040101
63                 if ( is_single() && isset($_GET['p']) ) {
64                         if ( $redirect_url = get_permalink(get_query_var('p')) )
65                                 $redirect['query'] = remove_query_arg('p', $redirect['query']);
66                 } elseif ( is_page() && isset($_GET['page_id']) ) {
67                         if ( $redirect_url = get_permalink(get_query_var('page_id')) )
68                                 $redirect['query'] = remove_query_arg('page_id', $redirect['query']);
69                 } elseif ( isset($_GET['m']) && ( is_year() || is_month() || is_day() ) ) {
70                         $m = get_query_var('m');
71                         switch ( strlen($m) ) {
72                                 case 4: // Yearly
73                                         $redirect_url = get_year_link($m);
74                                         break;
75                                 case 6: // Monthly
76                                         $redirect_url = get_month_link( substr($m, 0, 4), substr($m, 4, 2) );
77                                         break;
78                                 case 8: // Daily
79                                         $redirect_url = get_day_link(substr($m, 0, 4), substr($m, 4, 2), substr($m, 6, 2));
80                                         break;
81                         }
82                         if ( $redirect_url )
83                                 $redirect['query'] = remove_query_arg('m', $redirect['query']);
84                 // now moving on to non ?m=X year/month/day links
85                 } elseif ( is_day() && get_query_var('year') && get_query_var('monthnum') && isset($_GET['day']) ) {
86                         if ( $redirect_url = get_day_link(get_query_var('year'), get_query_var('monthnum'), get_query_var('day')) )
87                                 $redirect['query'] = remove_query_arg(array('year', 'monthnum', 'day'), $redirect['query']);
88                 } elseif ( is_month() && get_query_var('year') && isset($_GET['monthnum']) ) {
89                         if ( $redirect_url = get_month_link(get_query_var('year'), get_query_var('monthnum')) )
90                                 $redirect['query'] = remove_query_arg(array('year', 'monthnum'), $redirect['query']);
91                 } elseif ( is_year() && isset($_GET['year']) ) {
92                         if ( $redirect_url = get_year_link(get_query_var('year')) )
93                                 $redirect['query'] = remove_query_arg('year', $redirect['query']);
94                 } elseif ( is_category() && isset($_GET['cat']) ) {
95                         if ( $redirect_url = get_category_link(get_query_var('cat')) )
96                                 $redirect['query'] = remove_query_arg('cat', $redirect['query']);
97                 } elseif ( is_author() && isset($_GET['author']) ) {
98                         $author = get_userdata(get_query_var('author'));
99                         if ( false !== $author && $redirect_url = get_author_link(false, $author->ID, $author->user_nicename) )
100                                 $redirect['query'] = remove_query_arg('author', $redirect['author']);
101                 }
102
103         // paging
104                 if ( $paged = get_query_var('paged') ) {
105                         if ( $paged > 0 ) {
106                                 if ( !$redirect_url )
107                                         $redirect_url = $requested_url;
108                                 $paged_redirect = @parse_url($redirect_url);
109                                 $paged_redirect['path'] = preg_replace('|/page/[0-9]+?(/+)?$|', '/', $paged_redirect['path']); // strip off any existing paging
110                                 $paged_redirect['path'] = preg_replace('|/index.php/?$|', '/', $paged_redirect['path']); // strip off trailing /index.php/
111                                 if ( $paged > 1 && !is_single() ) {
112                                         $paged_redirect['path'] = trailingslashit($paged_redirect['path']);
113                                         if ( $wp_rewrite->using_index_permalinks() && strpos($paged_redirect['path'], '/index.php/') === false )
114                                                 $paged_redirect['path'] .= 'index.php/';
115                                         $paged_redirect['path'] .= user_trailingslashit("page/$paged", 'paged');
116                                 } elseif ( !is_home() && !is_single() ){
117                                         $paged_redirect['path'] = user_trailingslashit($paged_redirect['path'], 'paged');
118                                 }
119                                 $redirect_url = $paged_redirect['scheme'] . '://' . $paged_redirect['host'] . $paged_redirect['path'];
120                                 $redirect['path'] = $paged_redirect['path'];
121                         }
122                         $redirect['query'] = remove_query_arg('paged', $redirect['query']);
123                 }
124         }
125
126         // tack on any additional query vars
127         if ( $redirect_url && $redirect['query'] ) {
128                 if ( strpos($redirect_url, '?') !== false )
129                         $redirect_url .= '&';
130                 else
131                         $redirect_url .= '?';
132                 $redirect_url .= $redirect['query'];
133         }
134
135         if ( $redirect_url )
136                 $redirect = @parse_url($redirect_url);
137
138         // www.example.com vs example.com
139         $user_home = @parse_url(get_option('home'));
140         $redirect['host'] = $user_home['host'];
141
142         // Handle ports
143         if ( isset($user_home['port']) )
144                 $redirect['port'] = $user_home['port'];
145         else
146                 unset($redirect['port']);
147
148         // trailing /index.php/
149         $redirect['path'] = preg_replace('|/index.php/$|', '/', $redirect['path']);
150
151         // strip /index.php/ when we're not using PATHINFO permalinks
152         if ( !$wp_rewrite->using_index_permalinks() )
153                 $redirect['path'] = str_replace('/index.php/', '/', $redirect['path']);
154
155         // trailing slashes
156         if ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() && !is_404() && (!is_home() || ( is_home() && (get_query_var('paged') > 1) ) ) ) {
157                 $user_ts_type = '';
158                 if ( get_query_var('paged') > 0 ) {
159                         $user_ts_type = 'paged';
160                 } else {
161                         foreach ( array('single', 'category', 'page', 'day', 'month', 'year') as $type ) {
162                                 $func = 'is_' . $type;
163                                 if ( call_user_func($func) )
164                                         $user_ts_type = $type;
165                                         break;
166                                 }
167                         }
168                 $redirect['path'] = user_trailingslashit($redirect['path'], $user_ts_type);
169         } elseif ( is_home() ) {
170                 $redirect['path'] = trailingslashit($redirect['path']);
171         }
172
173         // Always trailing slash the 'home' URL
174         if ( $redirect['path'] == $user_home['path'] )
175                 $redirect['path'] = trailingslashit($redirect['path']);
176
177         // Ignore differences in host capitalization, as this can lead to infinite redirects
178         if ( strtolower($original['host']) == strtolower($redirect['host']) )
179                 $redirect['host'] = $original['host'];
180
181         if ( array($original['host'], $original['port'], $original['path'], $original['query']) !== array($redirect['host'], $redirect['port'], $redirect['path'], $redirect['query']) ) {
182                 $redirect_url = $redirect['scheme'] . '://' . $redirect['host'];
183                 if ( isset($redirect['port']) )
184                         $redirect_url .= ':' . $redirect['port'];
185                 $redirect_url .= $redirect['path'];
186                 if ( $redirect['query'] )
187                         $redirect_url .= '?' . $redirect['query'];
188         }
189
190         if ( !$redirect_url || $redirect_url == $requested_url )
191                 return false;
192
193         // Note that you can use the "redirect_canonical" filter to cancel a canonical redirect for whatever reason by returning FALSE
194         $redirect_url = apply_filters('redirect_canonical', $redirect_url, $requested_url);
195
196         if ( !$redirect_url || $redirect_url == $requested_url ) // yes, again -- in case the filter aborted the request
197                 return false;
198
199         if ( $do_redirect ) {
200                 // protect against chained redirects
201                 if ( !redirect_canonical($redirect_url, false) ) {
202                         wp_redirect($redirect_url, 301);
203                         exit();
204                 } else {
205                         return false;
206                 }
207         } else {
208                 return $redirect_url;
209         }
210 }
211
212 /**
213  * redirect_guess_404_permalink() - Tries to guess correct post based on query vars
214  *
215  * @since 2.3
216  * @uses $wpdb
217  *
218  * @return bool|string Returns False, if it can't find post, returns correct location on success.
219  */
220 function redirect_guess_404_permalink() {
221         global $wpdb;
222         if ( !get_query_var('name') )
223                 return false;
224
225         $where = $wpdb->prepare("post_name LIKE %s", get_query_var('name') . '%');
226
227         // if any of year, monthnum, or day are set, use them to refine the query
228         if ( get_query_var('year') )
229                 $where .= $wpdb->prepare(" AND YEAR(post_date) = %d", get_query_var('year'));
230         if ( get_query_var('monthnum') )
231                 $where .= $wpdb->prepare(" AND MONTH(post_date) = %d", get_query_var('monthnum'));
232         if ( get_query_var('day') )
233                 $where .= $wpdb->prepare(" AND DAYOFMONTH(post_date) = %d", get_query_var('day'));
234
235         $post_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE $where AND post_status = 'publish'");
236         if ( !$post_id )
237                 return false;
238         return get_permalink($post_id);
239 }
240
241 add_action('template_redirect', 'redirect_canonical');
242
243 ?>