Here's the plugin code. Thanks for all your help...
1. <?php 2. /* 3. * Plugin Name: Permalink Redirect 4. * Plugin URI: http://scott.yang.id.au/code/permalink-redirect/ 5. * Description: Permalink Redirect ensures that pages and entries are always accessed via the permalink. Otherwise, a 301 redirect will be issued. 6. * Version: 2.0 7. * Author: Scott Yang 8. * Author URI: http://scott.yang.id.au/ 9. */ 10. class YLSY_PermalinkRedirect { 11. function admin_menu() { 12. add_options_page('Permalink Redirect Manager', 'Permalink Redirect', 5, 13. __FILE__, array <http://www.php.net/array>($this, 'admin_page')); 14. } 15. 16. function admin_page() { 17. global <http://www.php.net/global> $wp_version; 18. 19. // If we are updating, we will flush all the rewrite rules to force the 20. // old structure to be added. 21. if (isset <http://www.php.net/isset>($_GET['updated'])) { 22. $this->regenerate_rules(); 23. } 24. 25. $options = array <http://www.php.net/array>('feedburner', 'feedburnerbrand', 'hostname', 26. 'oldstruct', 'skip', 'newpath'); 27. $optionvars = array <http://www.php.net/array>(); 28. foreach ($options as $option) { 29. $$option = get_option("permalink_redirect_$option"); 30. if (!$$option) { 31. $$option = ($option == 'feedburnerbrand') ? 32. 'feeds.feedburner.com' : ''; 33. } 34. if ($wp_version < '2' && !$$option) { 35. add_option("permalink_redirect_$option", $$option); 36. } 37. $optionvars[] = "permalink_redirect_$option"; 38. } 39. 40. $home = parse_url <http://www.php.net/parse_url>(get_option( 'home')); 41. ?> 42. <div class="wrap"> 43. <h2>Permalink Redirect Manager</h2> 44. <form action="options.php" method="post"> 45. <fieldset class="options"> 46. <legend>Paths to be skipped</legend> 47. <p>Separate each <http://www.php.net/each> entry with a new line. Matched with regular expression.</p> 48. <textarea name="permalink_redirect_skip" style= "width:98%;" rows="5"><?php echo <http://www.php.net/echo> htmlspecialchars <http://www.php.net/htmlspecialchars>($skip); ?></textarea> 49. 50. <legend style="padding-top:20px">Path pairs to redirect from and to</legend> 51. <p>Separate each <http://www.php.net/each> entry with a new line. Each <http://www.php.net/each> line is [from]<spaces>[to] .</p> 52. <textarea name="permalink_redirect_newpath" style= "width:98%;" rows="5"><?php echo <http://www.php.net/echo> htmlspecialchars <http://www.php.net/htmlspecialchars>($newpath); ?></textarea> 53. <table class="optiontable" style="padding-top:20px"> 54. <tr valign="top"> 55. <th scope="row">Old Permalink Structures:</th> 56. <td><textarea name="permalink_redirect_oldstruct"id= "permalink_redirect_oldstruct" style="width:98%" rows="3"><?php echo<http://www.php.net/echo> htmlspecialchars <http://www.php.net/htmlspecialchars>($oldstruct); ?></textarea><br/><small><a href=" http://codex.wordpress.org/Using_Permalinks">Available tags</a>. One Permalink Structure per line. Current <http://www.php.net/current>permalink structure: <a href= "#" onclick="document.getElementById('permalink_redirect_oldstruct').value = '<?php echo htmlspecialchars(get_option('permalink_structure')); ?>';return false;"><code><?php echo <http://www.php.net/echo> htmlspecialchars <http://www.php.net/htmlspecialchars>(get_option( 'permalink_structure')); ?></code></a></small></td> 57. </tr> 58. <tr> 59. <th scope="row">FeedBurner Redirect:</th> 60. <td>http://<input name="permalink_redirect_feedburnerbrand" type="text" id="permalink_redirect_feedburnerbrand" value="<?php print htmlspecialchars($feedburnerbrand); ?>" size="20"/>/<input name="permalink_redirect_feedburner" type="text" id="permalink_redirect_feedburner" value="<?php echo htmlspecialchars($feedburner) ?>" size="20" /></td> 61. </tr> 62. <tr> 63. <th scope="row">Hostname Redirect:</th> 64. <td><input name="permalink_redirect_hostname"type= "checkbox" id="permalink_redirect_hostname" value="1"<?php if ($hostname) { ?> checked="checked"<?php } ?>/> Redirect if hostname is not <code><?php echo <http://www.php.net/echo> htmlspecialchars<http://www.php.net/htmlspecialchars> ($home['host']); ?></code>.</td> 65. </tr> 66. </table> 67. </fieldset> 68. <p class="submit"> 69. <input type="submit" name="Submit" value="<?php _e('Update Options') ?> »" /> 70. <input type="hidden" name="action" value="update" /> 71. <input type="hidden" name="page_options" value="<?php echo join(',', $optionvars); ?>"/> 72. <?php if (function_exists<http://www.php.net/function_exists> ('wp_nonce_field')) { wp_nonce_field('update-options'); } ?> 73. </p> 74. </form> 75. </div> 76. <?php 77. } 78. 79. function check_hostname() { 80. if (! get_option('permalink_redirect_hostname')) { 81. return false; 82. } 83. $requested = $_SERVER['HTTP_HOST']; 84. $home = parse_url <http://www.php.net/parse_url>(get_option( 'home')); 85. return $requested != $home['host']; 86. } 87. 88. function execute() { 89. global <http://www.php.net/global> $wp_query; 90. $this->execute2($wp_query); 91. } 92. 93. function execute2($query, $testold=true) { 94. $req_uri = $_SERVER['REQUEST_URI']; 95. 96. if ($query->is_trackback || 97. $query->is_search || 98. $query->is_comments_popup || 99. $query->is_robots || 100. $this->is_skip($req_uri)) 101. { 102. return; 103. } 104. 105. $this->redirect_newpath($req_uri); 106. $this->redirect_feedburner($query); 107. 108. if ($query->is_404) { 109. if ($testold) { 110. $this->redirect_old_permalink($req_uri); 111. } 112. return; 113. } 114. 115. if (($req_uri = @parse_url <http://www.php.net/parse_url>( $_SERVER['REQUEST_URI'])) === false) { 116. return; 117. } 118. 119. $req_path = $req_uri['path']; 120. $new_uri = $this->guess_permalink($query); 121. if (!$new_uri) { 122. return; 123. } 124. $permalink = @parse_url <http://www.php.net/parse_url>( $new_uri); 125. 126. // WP2.1: If a static page has been set as the front-page, we'll get 127. // empty string here. 128. if (!$permalink['path']) { 129. $permalink['path'] = '/'; 130. } 131. if (($req_path != $permalink['path']) || $this-> check_hostname()) { 132. wp_redirect($new_uri, 301); 133. } 134. } 135. 136. function guess_permalink($query) { 137. $haspost = count <http://www.php.net/count>($query->posts) > 0; 138. if (get_query_var('m')) { 139. // Handling special case with '?m=yyyymmddHHMMSS' 140. // Since there is no code for producing the archive links for 141. // is_time, we will give up and not trying any redirection. 142. $m = preg_replace <http://www.php.net/preg_replace>( '/[^0-9]/', '', get_query_var('m')); 143. switch (strlen <http://www.php.net/strlen>($m)) { 144. case 4: // Yearly 145. $link = get_year_link($m); 146. break; 147. case 6: // Monthly 148. $link = get_month_link(substr<http://www.php.net/substr> ($m, 0, 4), substr <http://www.php.net/substr>($m, 4, 2)); 149. break; 150. case 8: // Daily 151. $link = get_day_link(substr<http://www.php.net/substr> ($m, 0, 4), substr <http://www.php.net/substr>($m, 4, 2), 152. substr<http://www.php.net/substr> ($m, 6, 2)); 153. break; 154. default: 155. return false; 156. } 157. } elseif (($query->is_single || $query->is_page) && $haspost ) { 158. $post = $query->posts[0]; 159. $link = get_permalink($post->ID); 160. $page = get_query_var('page'); 161. if ($page && $page > 1) 162. $link = trailingslashit($link) . "$page/"; 163. // WP2.2: In Wordpress 2.2+ is_home() returns false and is_page() 164. // returns true if front page is a static page. 165. if ($query->is_page && ('page' == get_option( 'show_on_front')) && 166. $post->ID == get_option('page_on_front')) 167. { 168. $link = trailingslashit($link); 169. } 170. } elseif ($query->is_author && $haspost) { 171. global <http://www.php.net/global> $wp_version; 172. if ($wp_version >= '2') { 173. $author = get_userdata(get_query_var('author')); 174. if ($author === false) 175. return false; 176. $link = get_author_link(false, $author->ID, 177. $author->user_nicename); 178. } else { 179. // XXX: get_author_link() bug in WP 1.5.1.2 180. // s/author_nicename/user_nicename/ 181. global <http://www.php.net/global> $cache_userdata; 182. $userid = get_query_var('author'); 183. $link = get_author_link(false, $userid, 184. $cache_userdata[$userid]->user_nicename); 185. } 186. } elseif ($query->is_category && $haspost) { 187. $link = get_category_link(get_query_var('cat')); 188. } elseif ($query->is_day && $haspost) { 189. $link = get_day_link(get_query_var('year'), 190. get_query_var('monthnum'), 191. get_query_var('day')); 192. } elseif ($query->is_month && $haspost) { 193. $link = get_month_link(get_query_var('year'), 194. get_query_var('monthnum')); 195. } elseif ($query->is_year && $haspost) { 196. $link = get_year_link(get_query_var('year')); 197. } elseif ($query->is_home) { 198. // WP2.1: Handling "Posts page" option. In WordPress 2.1 is_home() 199. // returns true and is_page() returns false if home page has been 200. // set to a page, and we are getting the permalink of that page 201. // here. 202. if ((get_option('show_on_front') == 'page') && 203. ($pageid = get_option('page_for_posts'))) 204. { 205. $link = trailingslashit(get_permalink($pageid)); 206. } else { 207. $link = trailingslashit(get_option('home')); 208. } 209. } else { 210. return false; 211. } 212. 213. if ($query->is_paged) { 214. $paged = get_query_var('paged'); 215. if ($paged) 216. $link = trailingslashit($link) . "page/$paged/"; 217. } 218. 219. if ($query->is_feed) { 220. $link = trailingslashit($link) . 'feed/'; 221. } 222. 223. return $link; 224. } 225. 226. function is_feedburner() { 227. return strncmp <http://www.php.net/strncmp>('FeedBurner/', $_SERVER['HTTP_USER_AGENT'], 11) == 0; 228. } 229. 230. function is_skip($path) { 231. $permalink_redirect_skip = get_option( 'permalink_redirect_skip'); 232. $permalink_redirect_skip = explode<http://www.php.net/explode> ("\n", $permalink_redirect_skip); 233. 234. // Apply 'permalink_redirect_skip' filter so other plugins can 235. // customise the skip behaviour. (Denis de Bernardy @ 2006-04-23) 236. $permalink_redirect_skip = apply_filters( 'permalink_redirect_skip', 237. $permalink_redirect_skip); 238. 239. foreach ($permalink_redirect_skip as $skip) { 240. $skip = trim <http://www.php.net/trim>($skip); 241. if ($skip && ereg <http://www.php.net/ereg>($skip, $path )) 242. return true; 243. } 244. 245. return false; 246. } 247. 248. function redirect_feedburner($query) { 249. // Check whether we need to do redirect for FeedBurner. 250. // NOTE this might not always get executed. For feeds, 251. // WP::send_headers() might send back a 304 before template_redirect 252. // action can be called. 253. global <http://www.php.net/global> $withcomments; 254. 255. if ($query->is_feed && !$query->is_archive && !$withcomments ) { 256. if (($feedburner = get_option( 'permalink_redirect_feedburner')) && 257. (strncmp <http://www.php.net/strncmp>('FeedBurner/', $_SERVER['HTTP_USER_AGENT'], 11) != 0)) 258. { 259. $brand = get_option( 'permalink_redirect_feedburnerbrand'); 260. $brand = $brand ? $brand : 'feeds.feedburner.com'; 261. wp_redirect("http://$brand/$feedburner", 302); 262. } 263. } 264. } 265. 266. // Static page redirect contributed by Sergey Menshikov. 267. function redirect_newpath($path) { 268. if ($newpathlist = get_option('permalink_redirect_newpath')) { 269. $newpathlist = explode <http://www.php.net/explode>("\n", $newpathlist); 270. foreach ($newpathlist as $newpath) { 271. $pair = preg_split <http://www.php.net/preg_split>( '/\s+/', trim <http://www.php.net/trim>($newpath)); 272. if ($pair[0] == $path) { 273. wp_redirect($pair[1], 301); 274. } 275. } 276. } 277. } 278. 279. /** 280. * Called when the main execute function gets a 404 to check against old 281. * permalink structures and perform redirect if an old post can be 282. * matched. 283. */ 284. function redirect_old_permalink($req_uri) { 285. global <http://www.php.net/global> $wp_query, $wp_rewrite; 286. global <http://www.php.net/global> $wp_version; 287. 288. $rules = get_option('permalink_redirect_rules'); 289. if (!$rules) { 290. return; 291. } 292. 293. // Backing up the rewrite object for you, imperative programmers! 294. $wp_rewrite_old = $wp_rewrite; 295. 296. // Unsetting the globals. Argh! Evil global variables! 297. foreach ($wp_query->query_vars as $key => $val) { 298. unset <http://www.php.net/unset>($GLOBALS[$key]); 299. } 300. 301. // Going through the rules. 302. foreach ($rules as $rules2) { 303. $wp2 = new WP(); 304. $wp_rewrite = new YLSY_Rewrite(); 305. $wp_rewrite->index = $wp_rewrite_old->index; 306. $wp_rewrite->rules = $rules2; 307. 308. $wp2->parse_request(); 309. if (isset <http://www.php.net/isset>($wp2->query_vars[ 'error']) && 310. ($wp2->query_vars['error'] == 404)) 311. { 312. continue; 313. } 314. $query = new WP_Query(); 315. if ($wp_version >= '2.1') { 316. $posts = $query->query($wp2->query_vars); 317. } else { 318. $wp2->build_query_string(); 319. $posts = $query->query($wp2->query_string); 320. } 321. if (count <http://www.php.net/count>($posts) > 0) { 322. $wp_rewrite = $wp_rewrite_old; 323. $this->execute2($query, false); 324. return; 325. } 326. } 327. 328. // Restoring global variables. We don't bother to reset the other 329. // variables as we are going to do a 404 anyway. 330. $wp_rewrite = $wp_rewrite_old; 331. } 332. 333. /** 334. * This function is called after someone saved the old permalink 335. * structure. It will create cached version of rewrite rules from the 336. * old structure. 337. */ 338. function regenerate_rules() { 339. global <http://www.php.net/global> $wp_rewrite; 340. $oldstruct = get_option('permalink_redirect_oldstruct'); 341. if ($oldstruct) { 342. $rules = array <http://www.php.net/array>(); 343. $oldstruct = explode <http://www.php.net/explode>("\n", $oldstruct); 344. foreach ($oldstruct as $item) { 345. $rules2 = $wp_rewrite->generate_rewrite_rule(trim<http://www.php.net/trim> ($item), 346. false, false, false, true); 347. $rules3 = array <http://www.php.net/array>(); 348. foreach ($rules2 as $match => $query) { 349. $query = preg_replace<http://www.php.net/preg_replace> ('/\$(\d+)/', '\$matches[\1]', $query); 350. $rules3[$match] = $query; 351. } 352. $rules[] = $rules3; 353. } 354. update_option('permalink_redirect_rules', $rules); 355. } else { 356. delete_option('permalink_redirect_rules'); 357. } 358. } 359. } 360. 361. /** 362. * I am a dummy class to simulate the WP_Rewite class, but only has one 363. * method implemented. 364. */ 365. class YLSY_Rewrite { 366. function wp_rewrite_rules() { 367. return $this->rules; 368. } 369. } 370. 371. if (!function_exists <http://www.php.net/function_exists>( 'wp_redirect')) { 372. function wp_redirect($location, $status=302) { 373. global <http://www.php.net/global> $is_IIS; 374. 375. $location = apply_filters('wp_redirect', $location, $status) ; 376. $status = apply_filters('wp_redirect_status', $status, $location); 377. 378. if (!$location) 379. return false; 380. 381. if (function_exists <http://www.php.net/function_exists>( 'wp_sanitize_redirect')) { 382. $location = wp_sanitize_redirect($location); 383. } 384. 385. if ($is_IIS) { 386. header <http://www.php.net/header>("Refresh: 0;url=$location"); 387. } else { 388. status_header($status); 389. header <http://www.php.net/header>("Location: $location" ); 390. } 391. } 392. } 393. 394. 395. $_permalink_redirect = new YLSY_PermalinkRedirect(); 396. add_action('admin_menu', array <http://www.php.net/array>( $_permalink_redirect, 'admin_menu')); 397. add_action('template_redirect', array <http://www.php.net/array>( $_permalink_redirect, 'execute')); On Mon, Aug 4, 2008 at 6:09 AM, John Blackbourn <[EMAIL PROTECTED]<[EMAIL PROTECTED]> > wrote: > Abhinav, > > Attachments get removed form emails to the list so you'll need to > upload the plugin and link to it instead. Try pastebin.com. > > Regards, > John. > > On Sun, Aug 3, 2008 at 3:10 PM, Abhinav Sood <[EMAIL PROTECTED]> > wrote: > > I have been facing this weird problem with my blog running Wordpress 2.6 > > Public release. > > > > I am on an IIS host and for a fairly long period was using the ugly > > permalink structure ( /?p=%post_id% ).. > > I wasn't satisfied with it and only recently found a way out. I have > managed > > to set up pretty permalinks on IIS > > host but then I had two link structures - the old ugly permalinks didn't > > redirect to pretty permalinks automatically > > so I risked losing my page rank and indexing in Google. > > > > To prevent that I used many redirection plugins but none worked > effectively > > - > > Except the one by Scott Yang (I have attached the file...) > > The problem is that the homepage keeps on reloading again and again, > > indefinitely > > (and so does the each page of sitemap reload automatically except the > first > > page...) > > > > my blog is www.inspiritblog.com and I'll be really grateful if you guys > can > > help in anyway > > > > -- > > Regards. > > Abhinav Sood > > > > www.inspiritblog.com | www.teenagerforums.net > > > > _______________________________________________ > > wp-testers mailing list > > [email protected] > > http://lists.automattic.com/mailman/listinfo/wp-testers > > > > > _______________________________________________ > wp-testers mailing list > [email protected] > http://lists.automattic.com/mailman/listinfo/wp-testers > > -- Regards. Abhinav Sood www.inspiritblog.com | www.teenagerforums.net _______________________________________________ wp-testers mailing list [email protected] http://lists.automattic.com/mailman/listinfo/wp-testers
