Make WordPress Core

Ticket #5017: canonical.php

File canonical.php, 7.8 KB (added by chaoticmortalcom, 17 years ago)

wp-includes/canonical.php

Line 
1<?php
2// Based on "Permalink Redirect" from Scott Yang and "Enforce www. Preference" by Mark Jaquith
3
4function redirect_canonical($requested_url=NULL, $do_redirect=true) {
5        global $wp_rewrite, $posts, $is_IIS;
6
7        if ( is_feed() || is_trackback() || is_search() || is_comments_popup() || is_admin() || $is_IIS || ( isset($_POST) && count($_POST) ) )
8                return;
9
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'];
15        }
16
17        $original = @parse_url($requested_url);
18        if ( false === $original )
19                return;
20
21        $redirect = $original;
22        $redirect_url = false;
23
24        // These tests give us a WP-generated permalink
25        if ( is_404() ) {
26                $redirect_url = redirect_guess_404_permalink();
27        } elseif ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() ) {
28                // rewriting of old ?p=X, ?m=2004, ?m=200401, ?m=20040101
29                if ( is_single() && isset($_GET['p']) ) {
30                        if ( $redirect_url = get_permalink(get_query_var('p')) )
31                                $redirect['query'] = remove_query_arg('p', $redirect['query']);
32                } elseif ( is_page() && isset($_GET['page_id']) ) {
33                        if ( $redirect_url = get_permalink(get_query_var('page_id')) )
34                                $redirect['query'] = remove_query_arg('page_id', $redirect['query']);
35                } elseif ( isset($_GET['m']) && ( is_year() || is_month() || is_day() ) ) {
36                        $m = get_query_var('m');
37                        switch ( strlen($m) ) {
38                                case 4: // Yearly
39                                        $redirect_url = get_year_link($m);
40                                        break;
41                                case 6: // Monthly
42                                        $redirect_url = get_month_link( substr($m, 0, 4), substr($m, 4, 2) );
43                                        break;
44                                case 8: // Daily
45                                        $redirect_url = get_day_link(substr($m, 0, 4), substr($m, 4, 2), substr($m, 6, 2));
46                                        break;
47                        }
48                        if ( $redirect_url )
49                                $redirect['query'] = remove_query_arg('m', $redirect['query']);
50                // now moving on to non ?m=X year/month/day links
51                } elseif ( is_day() && get_query_var('year') && get_query_var('monthnum') && isset($_GET['day']) ) {
52                        if ( $redirect_url = get_day_link(get_query_var('year'), get_query_var('monthnum'), get_query_var('day')) )
53                                $redirect['query'] = remove_query_arg(array('year', 'monthnum', 'day'), $redirect['query']);
54                } elseif ( is_month() && get_query_var('year') && isset($_GET['monthnum']) ) {
55                        if ( $redirect_url = get_month_link(get_query_var('year'), get_query_var('monthnum')) )
56                                $redirect['query'] = remove_query_arg(array('year', 'monthnum'), $redirect['query']);
57                } elseif ( is_year() && isset($_GET['year']) ) {
58                        if ( $redirect_url = get_year_link(get_query_var('year')) )
59                                $redirect['query'] = remove_query_arg('year', $redirect['query']);
60                } elseif ( is_category() && isset($_GET['cat']) ) {
61                        if ( $redirect_url = get_category_link(get_query_var('cat')) )
62                                $redirect['query'] = remove_query_arg('cat', $redirect['query']);
63                } elseif ( is_author() && isset($_GET['author']) ) {
64                        $author = get_userdata(get_query_var('author'));
65                        if ( false !== $author && $redirect_url = get_author_link(false, $author->ID, $author->user_nicename) )
66                                $redirect['query'] = remove_query_arg('author', $redirect['author']);
67                }
68
69        // paging
70                if ( $paged = get_query_var('paged') ) {
71                        if ( $paged > 0 ) {
72                                if ( !$redirect_url )
73                                        $redirect_url = $requested_url;
74                                $paged_redirect = @parse_url($redirect_url);
75                                $paged_redirect['path'] = preg_replace('|/page/[0-9]+?(/+)?$|', '/', $paged_redirect['path']); // strip off any existing paging
76                                $paged_redirect['path'] = preg_replace('|/index.php/?$|', '/', $paged_redirect['path']); // strip off trailing /index.php/
77                                if ( $paged > 1 && !is_single() ) {
78                                        $paged_redirect['path'] = trailingslashit($paged_redirect['path']);
79                                        if ( $wp_rewrite->using_index_permalinks() && strpos($paged_redirect['path'], '/index.php/') === false )
80                                                $paged_redirect['path'] .= 'index.php/';
81                                        $paged_redirect['path'] .= user_trailingslashit("page/$paged", 'paged');
82                                } elseif ( !is_home() && !is_single() ){
83                                        $paged_redirect['path'] = user_trailingslashit($paged_redirect['path'], 'paged');
84                                }
85                                $redirect_url = $paged_redirect['scheme'] . '://' . $paged_redirect['host'] . $paged_redirect['path'];
86                                $redirect['path'] = $paged_redirect['path'];
87                        }
88                        $redirect['query'] = remove_query_arg('paged', $redirect['query']);
89                }
90        }
91
92        // tack on any additional query vars
93        if ( $redirect_url && $redirect['query'] ) {
94                if ( strpos($redirect_url, '?') !== false )
95                        $redirect_url .= '&';
96                else
97                        $redirect_url .= '?';
98                $redirect_url .= $redirect['query'];
99        }
100
101        if ( $redirect_url )
102                $redirect = @parse_url($redirect_url);
103
104        // www.example.com vs example.com
105        $user_home = @parse_url(get_option('home'));
106        $redirect['host'] = $user_home['host'];
107
108        // Handle ports
109        if ( isset($user_home['port']) )
110                $redirect['port'] = $user_home['port'];
111        else
112                unset($redirect['port']);
113
114        // trailing /index.php or /index.php/
115        $redirect['path'] = preg_replace('|/index.php/?$|', '/', $redirect['path']);
116
117        // strip /index.php/ when we're not using PATHINFO permalinks
118        if ( !$wp_rewrite->using_index_permalinks() )
119                $redirect['path'] = str_replace('/index.php/', '/', $redirect['path']);
120
121        // trailing slashes
122        if ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() && !is_404() && (!is_home() || ( is_home() && (get_query_var('paged') > 1) ) ) ) {
123                $user_ts_type = '';
124                if ( get_query_var('paged') > 0 ) {
125                        $user_ts_type = 'paged';
126                } else {
127                        foreach ( array('single', 'category', 'page', 'day', 'month', 'year') as $type ) {
128                                $func = 'is_' . $type;
129                                if ( call_user_func($func) )
130                                        $user_ts_type = $type;
131                                        break;
132                                }
133                        }
134                $redirect['path'] = user_trailingslashit($redirect['path'], $user_ts_type);
135        } elseif ( is_home() ) {
136                $redirect['path'] = trailingslashit($redirect['path']);
137        }
138
139        // Always trailing slash the 'home' URL
140        if ( $redirect['path'] == $user_home['path'] )
141                $redirect['path'] = trailingslashit($redirect['path']);
142
143        // Ignore differences in host capitalization, as this can lead to infinite redirects
144        if ( strtolower($original['host']) == strtolower($redirect['host']) )
145                $redirect['host'] = $original['host'];
146
147        if ( array($original['host'], $original['port'], $original['path'], $original['query']) !== array($redirect['host'], $redirect['port'], $redirect['path'], $redirect['query']) ) {
148                $redirect_url = $redirect['scheme'] . '://' . $redirect['host'];
149                if ( isset($redirect['port']) )
150                        $redirect_url .= ':' . $redirect['port'];
151                $redirect_url .= $redirect['path'];
152                if ( $redirect['query'] )
153                        $redirect_url .= '?' . $redirect['query'];
154        }
155
156        if ( $redirect_url && $redirect_url != $requested_url ) {
157                // var_dump($redirect_url); die();
158                $redirect_url = apply_filters('redirect_canonical', $redirect_url, $requested_url);
159                if ( $do_redirect) {
160                        // protect against chained redirects
161                        if ( !redirect_canonical($redirect_url, false) ) {
162                                wp_redirect($redirect_url, 301);
163                                exit();
164                        } else {
165                                return false;
166                        }
167                } else {
168                        return $redirect_url;
169                }
170        } else {
171                return false;
172        }
173}
174
175function redirect_guess_404_permalink() {
176        global $wp_query, $wpdb;
177        if ( !get_query_var('name') )
178                return false;
179
180        $where = "post_name LIKE '" . $wpdb->escape(get_query_var('name')) . "%'";
181
182        // if any of year, monthnum, or day are set, use them to refine the query
183        if ( get_query_var('year') )
184                $where .= " AND YEAR(post_date) = '" . $wpdb->escape(get_query_var('year')) . "'";
185        if ( get_query_var('monthnum') )
186                $where .= " AND MONTH(post_date) = '" . $wpdb->escape(get_query_var('monthnum')) . "'";
187        if ( get_query_var('day') )
188                $where .= " AND DAYOFMONTH(post_date) = '" . $wpdb->escape(get_query_var('day')) . "'";
189
190        $post_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE $where AND post_status = 'publish'");
191        if ( !$post_id )
192                return false;
193        return get_permalink($post_id);
194}
195
196add_action('template_redirect', 'redirect_canonical');
197
198?>