1 | <?php
|
---|
2 | /*
|
---|
3 | Plugin Name: Permalink Validator
|
---|
4 | Plugin URI: http://wordpress.org/extend/plugins/permalink-validator/
|
---|
5 | Description: Validates the URL used and if not matching the official permalink then it issues a HTTP 301 or HTTP 404 message.
|
---|
6 | Author: Rolf Kristensen
|
---|
7 | Version: 0.7
|
---|
8 | Author URI: http://smallvoid.com/
|
---|
9 | */
|
---|
10 |
|
---|
11 | function permalink_validator_activate()
|
---|
12 | {
|
---|
13 | if (isset($_SERVER["REQUEST_METHOD"]) && $_SERVER["REQUEST_METHOD"] == 'POST')
|
---|
14 | return false;
|
---|
15 | if (defined('BBPATH'))
|
---|
16 | return false;
|
---|
17 | if ( is_404() )
|
---|
18 | return false;
|
---|
19 | if ( is_preview() )
|
---|
20 | return false;
|
---|
21 | if ( is_search() )
|
---|
22 | return false; // use noindex in header
|
---|
23 | if ( is_date() )
|
---|
24 | return false; // use noindex in header
|
---|
25 | if ( function_exists( 'is_tag' ) && is_tag() )
|
---|
26 | return false; // use noindex in header
|
---|
27 | if ( is_trackback() )
|
---|
28 | return false;
|
---|
29 | if ( is_admin() )
|
---|
30 | return false;
|
---|
31 | if ( is_feed() )
|
---|
32 | return false; // block feeds from being index'ed in robots.txt
|
---|
33 | if ( is_comments_popup() )
|
---|
34 | return false;
|
---|
35 | if ( function_exists( 'is_keyword' ) && is_keyword() )
|
---|
36 | return false; // Jeromeï¿œs Keywords (Use noindex in header)
|
---|
37 |
|
---|
38 | // Hack to make it easy to exclude one or more special page urls
|
---|
39 | // $excludes = array("/index.php/forum/");
|
---|
40 | // $excludes = array("/forum/", "/photos/");
|
---|
41 | $excludes = array();
|
---|
42 | foreach($excludes as $exclude) {
|
---|
43 | if (strpos($_SERVER['REQUEST_URI'],$exclude)==0) {
|
---|
44 | return;
|
---|
45 | }
|
---|
46 | }
|
---|
47 |
|
---|
48 | return true;
|
---|
49 | }
|
---|
50 |
|
---|
51 | // Is frontpage page of posts
|
---|
52 | function permalink_page_of_posts($page)
|
---|
53 | {
|
---|
54 | if (get_option('show_on_front') && get_option('page_for_posts') && strpos($page,get_page_link(get_option('page_for_posts')))!==FALSE)
|
---|
55 | return true;
|
---|
56 | else
|
---|
57 | return false;
|
---|
58 | }
|
---|
59 |
|
---|
60 | function permalink_validator() {
|
---|
61 | global $wp_query;
|
---|
62 | global $wp_rewrite;
|
---|
63 | global $posts;
|
---|
64 |
|
---|
65 | if (!permalink_validator_activate())
|
---|
66 | return;
|
---|
67 |
|
---|
68 | $page = ( isset($_SERVER['HTTPS'] ) && strtolower($_SERVER['HTTPS']) == 'on' ) ? 'https://' : 'http://';
|
---|
69 | $page .= $_SERVER['HTTP_HOST'];
|
---|
70 | $page .= $_SERVER['REQUEST_URI'];
|
---|
71 |
|
---|
72 | if ( is_home() )
|
---|
73 | {
|
---|
74 | $link = get_settings('home') . '/';
|
---|
75 | $page_of_posts = permalink_page_of_posts($page);
|
---|
76 | if ($page_of_posts===true)
|
---|
77 | $link = get_page_link(get_option('page_for_posts'));
|
---|
78 |
|
---|
79 | if (is_paged())
|
---|
80 | {
|
---|
81 | if (count($posts) < 1 || $posts[0]->ID=='')
|
---|
82 | {
|
---|
83 | $wp_query->set_404();
|
---|
84 | return;
|
---|
85 | }
|
---|
86 |
|
---|
87 | $paged = abs($wp_query->get('paged'));
|
---|
88 | if (!$wp_rewrite->using_permalinks())
|
---|
89 | {
|
---|
90 | if ($paged > 1)
|
---|
91 | {
|
---|
92 | if ($page_of_posts===true)
|
---|
93 | $link .= '&paged=' . $paged;
|
---|
94 | else
|
---|
95 | $link .= 'index.php?paged=' . $paged;
|
---|
96 | }
|
---|
97 | }
|
---|
98 | else
|
---|
99 | {
|
---|
100 | if ($paged > 1)
|
---|
101 | {
|
---|
102 | if ($wp_rewrite->using_index_permalinks() && $page_of_posts===false)
|
---|
103 | $link .= 'index.php/';
|
---|
104 | $link .= 'page/' . $paged . '/';
|
---|
105 | }
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 | if ($link != $page)
|
---|
110 | {
|
---|
111 | header('HTTP/1.1 301 Moved Permanently');
|
---|
112 | header('Status: 301 Moved Permanently');
|
---|
113 | header("Location: $link");
|
---|
114 | exit(0);
|
---|
115 | }
|
---|
116 | }
|
---|
117 | else if ( is_category() )
|
---|
118 | {
|
---|
119 | $cat_obj = $wp_query->get_queried_object();
|
---|
120 | if (!isset($cat_obj))
|
---|
121 | {
|
---|
122 | $wp_query->set_404();
|
---|
123 | return;
|
---|
124 | }
|
---|
125 |
|
---|
126 | $link = get_category_link($cat_obj->cat_ID);
|
---|
127 | if (is_paged())
|
---|
128 | {
|
---|
129 | if (count($posts) < 1 || $posts[0]->ID=='')
|
---|
130 | {
|
---|
131 | $wp_query->set_404();
|
---|
132 | return;
|
---|
133 | }
|
---|
134 |
|
---|
135 | $paged = abs($wp_query->get('paged'));
|
---|
136 | if (!$wp_rewrite->using_permalinks())
|
---|
137 | {
|
---|
138 | if ($paged > 1)
|
---|
139 | $link .= '&paged=' . $paged;
|
---|
140 | }
|
---|
141 | else
|
---|
142 | {
|
---|
143 | if ($paged > 1)
|
---|
144 | $link .= 'page/' . $paged . '/';
|
---|
145 | }
|
---|
146 | }
|
---|
147 |
|
---|
148 | if ($link!=$page)
|
---|
149 | {
|
---|
150 | header('HTTP/1.1 301 Moved Permanently');
|
---|
151 | header('Status: 301 Moved Permanently');
|
---|
152 | header("Location: $link");
|
---|
153 | exit(0);
|
---|
154 | }
|
---|
155 | }
|
---|
156 | else if ( is_single() )
|
---|
157 | {
|
---|
158 | if (count($posts) < 1 || $posts[0]->ID=='')
|
---|
159 | {
|
---|
160 | $wp_query->set_404();
|
---|
161 | return;
|
---|
162 | }
|
---|
163 |
|
---|
164 |
|
---|
165 | if (get_permalink($posts[0]->ID)!=$page)
|
---|
166 | {
|
---|
167 | $link = get_permalink($posts[0]->ID);
|
---|
168 | header('HTTP/1.1 301 Moved Permanently');
|
---|
169 | header('Status: 301 Moved Permanently');
|
---|
170 | header("Location: $link");
|
---|
171 | exit(0);
|
---|
172 | }
|
---|
173 | }
|
---|
174 | else if ( is_page() )
|
---|
175 | {
|
---|
176 | if (count($posts) < 1 || $posts[0]->ID=='')
|
---|
177 | {
|
---|
178 | $wp_query->set_404();
|
---|
179 | return;
|
---|
180 | }
|
---|
181 |
|
---|
182 | if (get_option('show_on_front')=='page' && get_option('page_on_front')==$posts[0]->ID)
|
---|
183 | {
|
---|
184 | // Static frontpage (WP 2.1+)
|
---|
185 | if ($page!=(get_settings('home') . '/'))
|
---|
186 | {
|
---|
187 | $link = get_settings('home');
|
---|
188 | header('HTTP/1.1 301 Moved Permanently');
|
---|
189 | header('Status: 301 Moved Permanently');
|
---|
190 | header("Location: $link");
|
---|
191 | exit(0);
|
---|
192 | }
|
---|
193 | return;
|
---|
194 | }
|
---|
195 |
|
---|
196 | if (get_page_link($posts[0]->ID)!=$page)
|
---|
197 | {
|
---|
198 | $link = get_page_link($posts[0]->ID);
|
---|
199 | header('HTTP/1.1 301 Moved Permanently');
|
---|
200 | header('Status: 301 Moved Permanently');
|
---|
201 | header("Location: $link");
|
---|
202 | exit(0);
|
---|
203 | }
|
---|
204 | }
|
---|
205 | else
|
---|
206 | {
|
---|
207 | return; // Unknown page-type (just pass it through)
|
---|
208 |
|
---|
209 | echo $_SERVER['REQUEST_URI'];
|
---|
210 | exit(0);
|
---|
211 |
|
---|
212 | $wp_query->set_404();
|
---|
213 | return;
|
---|
214 | }
|
---|
215 | return;
|
---|
216 | }
|
---|
217 |
|
---|
218 | function fix_request_uri_iis()
|
---|
219 | {
|
---|
220 | global $is_IIS;
|
---|
221 | global $wp_rewrite;
|
---|
222 |
|
---|
223 | // Only IIS is having problem with REQUEST_URI
|
---|
224 | if (!$is_IIS)
|
---|
225 | return;
|
---|
226 |
|
---|
227 | // Should only fix REQUEST_URI for pages where the validator is used
|
---|
228 | if (!permalink_validator_activate())
|
---|
229 | return;
|
---|
230 |
|
---|
231 | // IIS Mod-Rewrite
|
---|
232 | if (isset($_SERVER['HTTP_X_ORIGINAL_URL']))
|
---|
233 | {
|
---|
234 | $_SERVER['REQUEST_URI'] = $_SERVER['HTTP_X_ORIGINAL_URL'];
|
---|
235 | return;
|
---|
236 | }
|
---|
237 |
|
---|
238 | // IIS Isapi_Rewrite
|
---|
239 | if (isset($_SERVER['HTTP_X_REWRITE_URL']))
|
---|
240 | {
|
---|
241 | $_SERVER['REQUEST_URI'] = $_SERVER['HTTP_X_REWRITE_URL'];
|
---|
242 | return;
|
---|
243 | }
|
---|
244 |
|
---|
245 | // Use ORIG_PATH_INFO if there is no PATH_INFO
|
---|
246 | if ( !isset($_SERVER['PATH_INFO']) && isset($_SERVER['ORIG_PATH_INFO']) )
|
---|
247 | $_SERVER['PATH_INFO'] = $_SERVER['ORIG_PATH_INFO'];
|
---|
248 |
|
---|
249 | // Simulate REQUEST_URI on IIS
|
---|
250 | if (!empty($_SERVER['PATH_INFO']) && ($_SERVER['PATH_INFO'] == $_SERVER['SCRIPT_NAME']))
|
---|
251 | {
|
---|
252 | // Some IIS and PHP combinations puts the same value in PATH_INFO and SCRIPT_NAME
|
---|
253 | if ($wp_rewrite->using_permalinks())
|
---|
254 | $_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'];
|
---|
255 | else
|
---|
256 | $_SERVER['REQUEST_URI'] = substr($_SERVER['SCRIPT_NAME'], 0, strrpos($_SERVER['SCRIPT_NAME'], '/')) . '/';
|
---|
257 | }
|
---|
258 | else
|
---|
259 | {
|
---|
260 | // SCRIPT_NAME includes script-path + script-filename
|
---|
261 | if ($wp_rewrite->using_index_permalinks())
|
---|
262 | {
|
---|
263 | // If root then simulate that no script-name was specified
|
---|
264 | if (empty($_SERVER['PATH_INFO']))
|
---|
265 | $_SERVER['REQUEST_URI'] = substr($_SERVER['SCRIPT_NAME'], 0, strrpos($_SERVER['SCRIPT_NAME'], '/')) . '/';
|
---|
266 | else
|
---|
267 | $_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'] . $_SERVER['PATH_INFO'];
|
---|
268 | }
|
---|
269 | else
|
---|
270 | {
|
---|
271 | // If root then simulate that no script-name was specified
|
---|
272 | if (empty($_SERVER['PATH_INFO']))
|
---|
273 | $_SERVER['REQUEST_URI'] = substr($_SERVER['SCRIPT_NAME'], 0, strrpos($_SERVER['SCRIPT_NAME'], '/')) . '/';
|
---|
274 | else
|
---|
275 | $_SERVER['REQUEST_URI'] = substr($_SERVER['SCRIPT_NAME'], 0, strrpos($_SERVER['SCRIPT_NAME'], '/')) . $_SERVER['PATH_INFO'];
|
---|
276 | }
|
---|
277 | }
|
---|
278 |
|
---|
279 | // Append the query string if it exists and isn't null
|
---|
280 | if (isset($_SERVER['QUERY_STRING']) && !empty($_SERVER['QUERY_STRING']))
|
---|
281 | {
|
---|
282 | $_SERVER['REQUEST_URI'] .= '?' . $_SERVER['QUERY_STRING'];
|
---|
283 | }
|
---|
284 | }
|
---|
285 |
|
---|
286 | // Fix for missing slash in category- and page-links when not having trailing slash in post permalink structure (WP 2.2+)
|
---|
287 | function fix_user_trailingslashit($link, $type) {
|
---|
288 | global $wp_rewrite;
|
---|
289 | if ($wp_rewrite->using_permalinks() && $wp_rewrite->use_trailing_slashes==false && $type != 'single')
|
---|
290 | return trailingslashit($link);
|
---|
291 | return $link;
|
---|
292 | }
|
---|
293 |
|
---|
294 | if ( function_exists( 'add_filter' ) && function_exists('user_trailingslashit'))
|
---|
295 | {
|
---|
296 | add_filter('user_trailingslashit', 'fix_user_trailingslashit', 66, 2 );
|
---|
297 | }
|
---|
298 |
|
---|
299 | if ( function_exists( 'add_action' ) )
|
---|
300 | {
|
---|
301 | add_action('template_redirect', 'permalink_validator');
|
---|
302 | add_action('init', 'fix_request_uri_iis');
|
---|
303 | }
|
---|
304 | ?>
|
---|