3 * Canonical API to handle WordPress Redirecting
5 * Based on "Permalink Redirect" from Scott Yang and "Enforce www. Preference"
15 * Redirects incoming links to the proper URL based on the site url
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
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.
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.
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
39 function redirect_canonical($requested_url=null, $do_redirect=true) {
40 global $wp_rewrite, $is_IIS;
42 if ( is_feed() || is_trackback() || is_search() || is_comments_popup() || is_admin() || $is_IIS || ( isset($_POST) && count($_POST) ) || is_preview() || is_robots() )
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'];
52 $original = @parse_url($requested_url);
53 if ( false === $original )
56 // Some PHP setups turn requests for / into /index.php in REQUEST_URI
57 $original['path'] = preg_replace('|/index\.php$|', '/', $original['path']);
59 $redirect = $original;
60 $redirect_url = false;
62 // These tests give us a WP-generated permalink
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) ) {
77 $redirect_url = get_year_link($m);
80 $redirect_url = get_month_link( substr($m, 0, 4), substr($m, 4, 2) );
83 $redirect_url = get_day_link(substr($m, 0, 4), substr($m, 4, 2), substr($m, 6, 2));
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']);
108 if ( $paged = get_query_var('paged') ) {
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');
123 $redirect_url = $paged_redirect['scheme'] . '://' . $paged_redirect['host'] . $paged_redirect['path'];
124 $redirect['path'] = $paged_redirect['path'];
126 $redirect['query'] = remove_query_arg('paged', $redirect['query']);
130 // tack on any additional query vars
131 if ( $redirect_url && $redirect['query'] ) {
132 if ( strpos($redirect_url, '?') !== false )
133 $redirect_url .= '&';
135 $redirect_url .= '?';
136 $redirect_url .= $redirect['query'];
140 $redirect = @parse_url($redirect_url);
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'];
148 if ( isset($user_home['port']) )
149 $redirect['port'] = $user_home['port'];
151 unset($redirect['port']);
153 // trailing /index.php/
154 $redirect['path'] = preg_replace('|/index.php/$|', '/', $redirect['path']);
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']);
161 if ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() && !is_404() && (!is_home() || ( is_home() && (get_query_var('paged') > 1) ) ) ) {
163 if ( get_query_var('paged') > 0 ) {
164 $user_ts_type = 'paged';
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;
174 $redirect['path'] = user_trailingslashit($redirect['path'], $user_ts_type);
175 } elseif ( is_home() ) {
176 $redirect['path'] = trailingslashit($redirect['path']);
179 // Always trailing slash the 'home' URL
180 if ( $redirect['path'] == $user_home['path'] )
181 $redirect['path'] = trailingslashit($redirect['path']);
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'];
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'];
196 if ( !$redirect_url || $redirect_url == $requested_url )
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);
202 if ( !$redirect_url || $redirect_url == $requested_url ) // yes, again -- in case the filter aborted the request
205 if ( $do_redirect ) {
206 // protect against chained redirects
207 if ( !redirect_canonical($redirect_url, false) ) {
208 wp_redirect($redirect_url, 301);
214 return $redirect_url;
219 * Attempts to guess correct post based on query vars
224 * @return bool|string Returns False, if it can't find post, returns correct
225 * location on success.
227 function redirect_guess_404_permalink() {
229 if ( !get_query_var('name') )
232 $where = $wpdb->prepare("post_name LIKE %s", get_query_var('name') . '%');
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'));
242 $post_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE $where AND post_status = 'publish'");
245 return get_permalink($post_id);
248 add_action('template_redirect', 'redirect_canonical');