2 // Based on "Permalink Redirect" from Scott Yang and "Enforce www. Preference" by Mark Jaquith
4 function redirect_canonical($requested_url=NULL, $do_redirect=true) {
5 global $wp_rewrite, $posts, $is_IIS;
7 if ( is_feed() || is_trackback() || is_search() || is_comments_popup() || is_admin() || $is_IIS || ( isset($_POST) && count($_POST) ) || is_preview() )
10 if ( !$requested_url ) {
11 // build the URL in the address bar
12 $requested_url = ( isset($_SERVER['HTTPS'] ) && strtolower($_SERVER['HTTPS']) == 'on' ) ? 'https://' : 'http://';
13 $requested_url .= $_SERVER['HTTP_HOST'];
14 $requested_url .= $_SERVER['REQUEST_URI'];
17 $original = @parse_url($requested_url);
18 if ( false === $original )
21 // Some PHP setups turn requests for / into /index.php in REQUEST_URI
22 $original['path'] = preg_replace('|/index\.php$|', '/', $original['path']);
24 $redirect = $original;
25 $redirect_url = false;
27 // These tests give us a WP-generated permalink
29 $redirect_url = redirect_guess_404_permalink();
30 } elseif ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() ) {
31 // rewriting of old ?p=X, ?m=2004, ?m=200401, ?m=20040101
32 if ( is_single() && isset($_GET['p']) ) {
33 if ( $redirect_url = get_permalink(get_query_var('p')) )
34 $redirect['query'] = remove_query_arg('p', $redirect['query']);
35 } elseif ( is_page() && isset($_GET['page_id']) ) {
36 if ( $redirect_url = get_permalink(get_query_var('page_id')) )
37 $redirect['query'] = remove_query_arg('page_id', $redirect['query']);
38 } elseif ( isset($_GET['m']) && ( is_year() || is_month() || is_day() ) ) {
39 $m = get_query_var('m');
40 switch ( strlen($m) ) {
42 $redirect_url = get_year_link($m);
45 $redirect_url = get_month_link( substr($m, 0, 4), substr($m, 4, 2) );
48 $redirect_url = get_day_link(substr($m, 0, 4), substr($m, 4, 2), substr($m, 6, 2));
52 $redirect['query'] = remove_query_arg('m', $redirect['query']);
53 // now moving on to non ?m=X year/month/day links
54 } elseif ( is_day() && get_query_var('year') && get_query_var('monthnum') && isset($_GET['day']) ) {
55 if ( $redirect_url = get_day_link(get_query_var('year'), get_query_var('monthnum'), get_query_var('day')) )
56 $redirect['query'] = remove_query_arg(array('year', 'monthnum', 'day'), $redirect['query']);
57 } elseif ( is_month() && get_query_var('year') && isset($_GET['monthnum']) ) {
58 if ( $redirect_url = get_month_link(get_query_var('year'), get_query_var('monthnum')) )
59 $redirect['query'] = remove_query_arg(array('year', 'monthnum'), $redirect['query']);
60 } elseif ( is_year() && isset($_GET['year']) ) {
61 if ( $redirect_url = get_year_link(get_query_var('year')) )
62 $redirect['query'] = remove_query_arg('year', $redirect['query']);
63 } elseif ( is_category() && isset($_GET['cat']) ) {
64 if ( $redirect_url = get_category_link(get_query_var('cat')) )
65 $redirect['query'] = remove_query_arg('cat', $redirect['query']);
66 } elseif ( is_author() && isset($_GET['author']) ) {
67 $author = get_userdata(get_query_var('author'));
68 if ( false !== $author && $redirect_url = get_author_link(false, $author->ID, $author->user_nicename) )
69 $redirect['query'] = remove_query_arg('author', $redirect['author']);
73 if ( $paged = get_query_var('paged') ) {
76 $redirect_url = $requested_url;
77 $paged_redirect = @parse_url($redirect_url);
78 $paged_redirect['path'] = preg_replace('|/page/[0-9]+?(/+)?$|', '/', $paged_redirect['path']); // strip off any existing paging
79 $paged_redirect['path'] = preg_replace('|/index.php/?$|', '/', $paged_redirect['path']); // strip off trailing /index.php/
80 if ( $paged > 1 && !is_single() ) {
81 $paged_redirect['path'] = trailingslashit($paged_redirect['path']);
82 if ( $wp_rewrite->using_index_permalinks() && strpos($paged_redirect['path'], '/index.php/') === false )
83 $paged_redirect['path'] .= 'index.php/';
84 $paged_redirect['path'] .= user_trailingslashit("page/$paged", 'paged');
85 } elseif ( !is_home() && !is_single() ){
86 $paged_redirect['path'] = user_trailingslashit($paged_redirect['path'], 'paged');
88 $redirect_url = $paged_redirect['scheme'] . '://' . $paged_redirect['host'] . $paged_redirect['path'];
89 $redirect['path'] = $paged_redirect['path'];
91 $redirect['query'] = remove_query_arg('paged', $redirect['query']);
95 // tack on any additional query vars
96 if ( $redirect_url && $redirect['query'] ) {
97 if ( strpos($redirect_url, '?') !== false )
100 $redirect_url .= '?';
101 $redirect_url .= $redirect['query'];
105 $redirect = @parse_url($redirect_url);
107 // www.example.com vs example.com
108 $user_home = @parse_url(get_option('home'));
109 $redirect['host'] = $user_home['host'];
112 if ( isset($user_home['port']) )
113 $redirect['port'] = $user_home['port'];
115 unset($redirect['port']);
117 // trailing /index.php/
118 $redirect['path'] = preg_replace('|/index.php/$|', '/', $redirect['path']);
120 // strip /index.php/ when we're not using PATHINFO permalinks
121 if ( !$wp_rewrite->using_index_permalinks() )
122 $redirect['path'] = str_replace('/index.php/', '/', $redirect['path']);
125 if ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() && !is_404() && (!is_home() || ( is_home() && (get_query_var('paged') > 1) ) ) ) {
127 if ( get_query_var('paged') > 0 ) {
128 $user_ts_type = 'paged';
130 foreach ( array('single', 'category', 'page', 'day', 'month', 'year') as $type ) {
131 $func = 'is_' . $type;
132 if ( call_user_func($func) )
133 $user_ts_type = $type;
137 $redirect['path'] = user_trailingslashit($redirect['path'], $user_ts_type);
138 } elseif ( is_home() ) {
139 $redirect['path'] = trailingslashit($redirect['path']);
142 // Always trailing slash the 'home' URL
143 if ( $redirect['path'] == $user_home['path'] )
144 $redirect['path'] = trailingslashit($redirect['path']);
146 // Ignore differences in host capitalization, as this can lead to infinite redirects
147 if ( strtolower($original['host']) == strtolower($redirect['host']) )
148 $redirect['host'] = $original['host'];
150 if ( array($original['host'], $original['port'], $original['path'], $original['query']) !== array($redirect['host'], $redirect['port'], $redirect['path'], $redirect['query']) ) {
151 $redirect_url = $redirect['scheme'] . '://' . $redirect['host'];
152 if ( isset($redirect['port']) )
153 $redirect_url .= ':' . $redirect['port'];
154 $redirect_url .= $redirect['path'];
155 if ( $redirect['query'] )
156 $redirect_url .= '?' . $redirect['query'];
159 if ( $redirect_url && $redirect_url != $requested_url ) {
160 // var_dump($redirect_url); die();
161 $redirect_url = apply_filters('redirect_canonical', $redirect_url, $requested_url);
163 // protect against chained redirects
164 if ( !redirect_canonical($redirect_url, false) ) {
165 wp_redirect($redirect_url, 301);
171 return $redirect_url;
178 function redirect_guess_404_permalink() {
179 global $wp_query, $wpdb;
180 if ( !get_query_var('name') )
183 $where = "post_name LIKE '" . $wpdb->escape(get_query_var('name')) . "%'";
185 // if any of year, monthnum, or day are set, use them to refine the query
186 if ( get_query_var('year') )
187 $where .= " AND YEAR(post_date) = '" . $wpdb->escape(get_query_var('year')) . "'";
188 if ( get_query_var('monthnum') )
189 $where .= " AND MONTH(post_date) = '" . $wpdb->escape(get_query_var('monthnum')) . "'";
190 if ( get_query_var('day') )
191 $where .= " AND DAYOFMONTH(post_date) = '" . $wpdb->escape(get_query_var('day')) . "'";
193 $post_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE $where AND post_status = 'publish'");
196 return get_permalink($post_id);
199 add_action('template_redirect', 'redirect_canonical');