Simple 301 Redirects - Version 1.05

Version Description

  • Wildcard support
  • Delete functiontionality
  • On-page documentation
Download this release

Release Info

Developer scottnelle
Plugin Icon 128x128 Simple 301 Redirects
Version 1.05
Comparing to
See all releases

Code changes from version 1.03 to 1.05

Files changed (3) hide show
  1. readme.txt +16 -9
  2. screenshot-1.jpg +0 -0
  3. wp-simple-301-redirects.php +222 -72
readme.txt CHANGED
@@ -1,9 +1,9 @@
1
  === Simple 301 Redirects ===
2
  Contributors: scottnelle
3
  Tags: 301, redirect, url, seo
4
- Requires at least: 1.5
5
- Tested up to: 3.0
6
- Stable tag: 1.03
7
 
8
  Simple 301 Redirects provides an easy method of redirecting requests to another page on your site or elsewhere on the web.
9
 
@@ -11,7 +11,8 @@ Simple 301 Redirects provides an easy method of redirecting requests to another
11
 
12
  Simple 301 Redirects provides an easy method of redirecting requests to another page on your site or elsewhere on the web. It's especially handy when you migrate a site to WordPress and can't preserve your URL structure. By setting up 301 redirects from your old pages to your new pages, any incoming links will be seemlessly passed along, and their pagerank (or what-have-you) will be passed along with them.
13
 
14
- Note: The format for requests is '/about.htm' and the format for redirects is 'http://www.domain.com/about/' in order to emulate the way Apache handles 301 redirects and because while you can only accept requests to your site, you can redirect to anywhere.
 
15
 
16
  == Installation ==
17
 
@@ -19,21 +20,27 @@ Note: The format for requests is '/about.htm' and the format for redirects is 'h
19
  1. Activate the plugin through the 'Plugins' menu in WordPress
20
  1. Add redirects on the Settings > 301 Redirects page.
21
 
22
- == Screenshots ==
23
-
24
- 1. The admin interface
25
 
26
  == Changelog ==
27
 
 
 
 
 
 
 
 
 
 
 
 
28
  = 1.03 =
29
  * Sorry for the double update. I forgot to check for PHP4 compatibility. Many people are still using PHP4, apparently, so this update is to fix compatibility with these legacy systems.
30
 
31
-
32
  = 1.02 =
33
  * Added support for special characters in non-english URLs.
34
  * Fixed a case sensitivity bug.
35
 
36
-
37
  = 1.01 =
38
  * Updated redirect method to send headers directly rather than using wp_redirect() because it was sending 302 codes on some servers
39
 
1
  === Simple 301 Redirects ===
2
  Contributors: scottnelle
3
  Tags: 301, redirect, url, seo
4
+ Requires at least: 3.0
5
+ Tested up to: 3.6.1
6
+ Stable tag: 1.05
7
 
8
  Simple 301 Redirects provides an easy method of redirecting requests to another page on your site or elsewhere on the web.
9
 
11
 
12
  Simple 301 Redirects provides an easy method of redirecting requests to another page on your site or elsewhere on the web. It's especially handy when you migrate a site to WordPress and can't preserve your URL structure. By setting up 301 redirects from your old pages to your new pages, any incoming links will be seemlessly passed along, and their pagerank (or what-have-you) will be passed along with them.
13
 
14
+ New for version 1.05: Wildcard redirect support.
15
+
16
 
17
  == Installation ==
18
 
20
  1. Activate the plugin through the 'Plugins' menu in WordPress
21
  1. Add redirects on the Settings > 301 Redirects page.
22
 
 
 
 
23
 
24
  == Changelog ==
25
 
26
+ = 1.05 =
27
+ * Wildcard support
28
+ * Delete functiontionality
29
+ * On-page documentation
30
+
31
+ = 1.04 =
32
+ * Removed deprecated function calls
33
+ * Updated code to better match WordPress coding standards
34
+ * Updated handling of https protocol to prevent errors in certain server configurations
35
+ * Better support for destinations that start with a leading slash instead of the full domain
36
+
37
  = 1.03 =
38
  * Sorry for the double update. I forgot to check for PHP4 compatibility. Many people are still using PHP4, apparently, so this update is to fix compatibility with these legacy systems.
39
 
 
40
  = 1.02 =
41
  * Added support for special characters in non-english URLs.
42
  * Fixed a case sensitivity bug.
43
 
 
44
  = 1.01 =
45
  * Updated redirect method to send headers directly rather than using wp_redirect() because it was sending 302 codes on some servers
46
 
screenshot-1.jpg DELETED
Binary file
wp-simple-301-redirects.php CHANGED
@@ -2,13 +2,13 @@
2
  /*
3
  Plugin Name: Simple 301 Redirects
4
  Plugin URI: http://www.scottnelle.com/simple-301-redirects-plugin-for-wordpress/
5
- Description: Create a list of URLs that you would like to 301 redirect to another page or site
6
- Version: 1.03
7
  Author: Scott Nellé
8
  Author URI: http://www.scottnelle.com/
9
  */
10
 
11
- /* Copyright 2009 Scott Nellé (email : theguy@scottnelle.com)
12
 
13
  This program is free software; you can redistribute it and/or modify
14
  it under the terms of the GNU General Public License as published by
@@ -26,54 +26,128 @@ Author URI: http://www.scottnelle.com/
26
  */
27
 
28
  if (!class_exists("Simple301redirects")) {
 
29
  class Simple301Redirects {
30
- /*
31
- generate the link to the options page under settings
32
- */
33
- function create_menu()
34
- {
35
- add_options_page('301 Redirects', '301 Redirects', 10, '301options', array($this,'options_page'));
 
 
 
36
  }
37
 
38
- /*
39
- generate the options page in the wordpress admin
40
- */
41
- function options_page()
42
- {
 
 
43
  ?>
44
- <div class="wrap">
45
- <h2>Simple 301 Redirects</h2>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
 
47
- <form method="post" action="options-general.php?page=301options">
 
 
 
 
48
 
49
- <table>
50
- <tr>
51
- <th>Request</th>
52
- <th>Destination</th>
53
- </tr>
54
- <tr>
55
- <td><small>example: /about.htm</small></td>
56
- <td><small>example: <?php echo get_option('home'); ?>/about/</small></td>
57
- </tr>
58
- <?php echo $this->expand_redirects(); ?>
59
- <tr>
60
- <td><input type="text" name="301_redirects[request][]" value="" style="width:15em" />&nbsp;&raquo;&nbsp;</td>
61
- <td><input type="text" name="301_redirects[destination][]" value="" style="width:30em;" /></td>
62
- </tr>
63
- </table>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
 
65
- <p class="submit">
66
- <input type="submit" name="submit_301" class="button-primary" value="<?php _e('Save Changes') ?>" />
67
- </p>
68
- </form>
 
 
 
 
 
 
 
 
69
  </div>
70
  <?php
71
  } // end of function options_page
72
 
73
- /*
74
- utility function to return the current list of redirects as form fields
75
- */
76
- function expand_redirects(){
 
 
 
77
  $redirects = get_option('301_redirects');
78
  $output = '';
79
  if (!empty($redirects)) {
@@ -81,8 +155,10 @@ if (!class_exists("Simple301redirects")) {
81
  $output .= '
82
 
83
  <tr>
84
- <td><input type="text" name="301_redirects[request][]" value="'.$request.'" style="width:15em" />&nbsp;&raquo;&nbsp;</td>
85
- <td><input type="text" name="301_redirects[destination][]" value="'.$destination.'" style="width:30em;" /></td>
 
 
86
  </tr>
87
 
88
  ';
@@ -91,11 +167,14 @@ if (!class_exists("Simple301redirects")) {
91
  return $output;
92
  }
93
 
94
- /*
95
- save the redirects from the options page to the database
96
- */
97
- function save_redirects($data)
98
- {
 
 
 
99
  $redirects = array();
100
 
101
  for($i = 0; $i < sizeof($data['request']); ++$i) {
@@ -105,27 +184,88 @@ if (!class_exists("Simple301redirects")) {
105
  if ($request == '' && $destination == '') { continue; }
106
  else { $redirects[$request] = $destination; }
107
  }
108
-
109
  update_option('301_redirects', $redirects);
 
 
 
 
 
 
 
110
  }
111
 
112
- /*
113
- Read the list of redirects and if the current page
114
- is found in the list, send the visitor on her way
115
- */
116
- function redirect()
117
- {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
118
  // this is what the user asked for (strip out home portion, case insensitive)
119
- $userrequest = str_ireplace(get_option('home'),'',$this->getAddress());
120
  $userrequest = rtrim($userrequest,'/');
121
 
122
  $redirects = get_option('301_redirects');
123
  if (!empty($redirects)) {
 
 
 
 
 
124
  foreach ($redirects as $storedrequest => $destination) {
125
- // compare user request to each 301 stored in the db
126
- if(urldecode($userrequest) == rtrim($storedrequest,'/')) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
127
  header ('HTTP/1.1 301 Moved Permanently');
128
- header ('Location: ' . $destination);
129
  exit();
130
  }
131
  else { unset($redirects); }
@@ -133,17 +273,28 @@ if (!class_exists("Simple301redirects")) {
133
  }
134
  } // end funcion redirect
135
 
136
- /*
137
- utility function to get the full address of the current request
138
- credit: http://www.phpro.org/examples/Get-Full-URL.html
139
- */
140
- function getAddress()
141
- {
142
- /*** check for https ***/
143
- $protocol = $_SERVER['HTTPS'] == 'on' ? 'https' : 'http';
144
- /*** return the full address ***/
145
- return $protocol.'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
146
- }
 
 
 
 
 
 
 
 
 
 
 
147
 
148
  } // end class Simple301Redirects
149
 
@@ -160,7 +311,7 @@ if (isset($redirect_plugin)) {
160
  add_action('admin_menu', array($redirect_plugin,'create_menu'));
161
 
162
  // if submitted, process the data
163
- if (isset($_POST['submit_301'])) {
164
  $redirect_plugin->save_redirects($_POST['301_redirects']);
165
  }
166
  }
@@ -178,5 +329,4 @@ if(!function_exists('str_ireplace')){
178
  $subject = str_replace($token,$replace,$subject);
179
  return $subject;
180
  }
181
- }
182
- ?>
2
  /*
3
  Plugin Name: Simple 301 Redirects
4
  Plugin URI: http://www.scottnelle.com/simple-301-redirects-plugin-for-wordpress/
5
+ Description: Create a list of URLs that you would like to 301 redirect to another page or site. Now with wildcard support.
6
+ Version: 1.05
7
  Author: Scott Nellé
8
  Author URI: http://www.scottnelle.com/
9
  */
10
 
11
+ /* Copyright 2009-2013 Scott Nellé (email : contact@scottnelle.com)
12
 
13
  This program is free software; you can redistribute it and/or modify
14
  it under the terms of the GNU General Public License as published by
26
  */
27
 
28
  if (!class_exists("Simple301redirects")) {
29
+
30
  class Simple301Redirects {
31
+
32
+ /**
33
+ * create_menu function
34
+ * generate the link to the options page under settings
35
+ * @access public
36
+ * @return void
37
+ */
38
+ function create_menu() {
39
+ add_options_page('301 Redirects', '301 Redirects', 'manage_options', '301options', array($this,'options_page'));
40
  }
41
 
42
+ /**
43
+ * options_page function
44
+ * generate the options page in the wordpress admin
45
+ * @access public
46
+ * @return void
47
+ */
48
+ function options_page() {
49
  ?>
50
+ <div class="wrap simple_301_redirects">
51
+ <script>
52
+ //todo: This should be enqued
53
+ jQuery(document).ready(function(){
54
+ jQuery('span.wps301-delete').html('Delete').css({'color':'red','cursor':'pointer'}).click(function(){
55
+ var confirm_delete = confirm('Delete This Redirect?');
56
+ if (confirm_delete) {
57
+
58
+ // remove element and submit
59
+ jQuery(this).parent().parent().remove();
60
+ jQuery('#simple_301_redirects_form').submit();
61
+
62
+ }
63
+ });
64
+
65
+ jQuery('.simple_301_redirects .documentation').hide().before('<p><a class="reveal-documentation" href="#">Documentation</a></p>')
66
+ jQuery('.reveal-documentation').click(function(){
67
+ jQuery(this).parent().siblings('.documentation').slideToggle();
68
+ return false;
69
+ });
70
+ });
71
+ </script>
72
 
73
+ <?php
74
+ if (isset($_POST['301_redirects'])) {
75
+ echo '<div id="message" class="updated"><p>Settings saved</p></div>';
76
+ }
77
+ ?>
78
 
79
+ <h2>Simple 301 Redirects</h2>
80
+
81
+ <form method="post" id="simple_301_redirects_form" action="options-general.php?page=301options&savedata=true">
82
+
83
+ <table class="widefat">
84
+ <thead>
85
+ <tr>
86
+ <th colspan="2">Request</th>
87
+ <th colspan="2">Destination</th>
88
+ </tr>
89
+ </thead>
90
+ <tbody>
91
+ <tr>
92
+ <td colspan="2"><small>example: /about.htm</small></td>
93
+ <td colspan="2"><small>example: <?php echo get_option('home'); ?>/about/</small></td>
94
+ </tr>
95
+ <?php echo $this->expand_redirects(); ?>
96
+ <tr>
97
+ <td style="width:35%;"><input type="text" name="301_redirects[request][]" value="" style="width:99%;" /></td>
98
+ <td style="width:2%;">&raquo;</td>
99
+ <td style="width:60%;"><input type="text" name="301_redirects[destination][]" value="" style="width:99%;" /></td>
100
+ <td><span class="wps301-delete">Delete</span></td>
101
+ </tr>
102
+ </tbody>
103
+ </table>
104
+
105
+ <?php $wildcard_checked = (get_option('301_redirects_wildcard') === 'true' ? ' checked="checked"' : ''); ?>
106
+ <p><input type="checkbox" name="301_redirects[wildcard]" id="wps301-wildcard"<?php echo $wildcard_checked; ?> /><label for="wps301-wildcard"> Use Wildcards?</label></p>
107
+
108
+ <p class="submit"><input type="submit" name="submit_301" class="button-primary" value="<?php _e('Save Changes') ?>" /></p>
109
+ </form>
110
+ <div class="documentation">
111
+ <h2>Documentation</h2>
112
+ <h3>Simple Redirects</h3>
113
+ <p>Simple redirects work similar to the format that Apache uses: the request should be relative to your WordPress root. The destination can be either a full URL to any page on the web, or relative to your WordPress root.</p>
114
+ <h4>Example</h4>
115
+ <ul>
116
+ <li><strong>Request:</strong> /old-page/</li>
117
+ <li><strong>Destination:</strong> /new-page/</li>
118
+ </ul>
119
+
120
+ <h3>Wildcards</h3>
121
+ <p>To use wildcards, put an asterisk (*) after the folder name that you want to redirect.</p>
122
+ <h4>Example</h4>
123
+ <ul>
124
+ <li><strong>Request:</strong> /old-folder/*</li>
125
+ <li><strong>Destination:</strong> /redirect-everything-here/</li>
126
+ </ul>
127
 
128
+ <p>You can also use the asterisk in the destination to replace whatever it matched in the request if you like. Something like this:</p>
129
+ <h4>Example</h4>
130
+ <ul>
131
+ <li><strong>Request:</strong> /old-folder/*</li>
132
+ <li><strong>Destination:</strong> /some/other/folder/*</li>
133
+ </ul>
134
+ <p>Or:</p>
135
+ <ul>
136
+ <li><strong>Request:</strong> /old-folder/*/content/</li>
137
+ <li><strong>Destination:</strong> /some/other/folder/*</li>
138
+ </ul>
139
+ </div>
140
  </div>
141
  <?php
142
  } // end of function options_page
143
 
144
+ /**
145
+ * expand_redirects function
146
+ * utility function to return the current list of redirects as form fields
147
+ * @access public
148
+ * @return string <html>
149
+ */
150
+ function expand_redirects() {
151
  $redirects = get_option('301_redirects');
152
  $output = '';
153
  if (!empty($redirects)) {
155
  $output .= '
156
 
157
  <tr>
158
+ <td><input type="text" name="301_redirects[request][]" value="'.$request.'" style="width:99%" /></td>
159
+ <td>&raquo;</td>
160
+ <td><input type="text" name="301_redirects[destination][]" value="'.$destination.'" style="width:99%;" /></td>
161
+ <td><span class="wps301-delete"></span></td>
162
  </tr>
163
 
164
  ';
167
  return $output;
168
  }
169
 
170
+ /**
171
+ * save_redirects function
172
+ * save the redirects from the options page to the database
173
+ * @access public
174
+ * @param mixed $data
175
+ * @return void
176
+ */
177
+ function save_redirects($data) {
178
  $redirects = array();
179
 
180
  for($i = 0; $i < sizeof($data['request']); ++$i) {
184
  if ($request == '' && $destination == '') { continue; }
185
  else { $redirects[$request] = $destination; }
186
  }
187
+
188
  update_option('301_redirects', $redirects);
189
+
190
+ if (isset($data['wildcard'])) {
191
+ update_option('301_redirects_wildcard', 'true');
192
+ }
193
+ else {
194
+ delete_option('301_redirects_wildcard');
195
+ }
196
  }
197
 
198
+ /**
199
+ * delete_redirect function
200
+ * save the redirects from the options page to the database
201
+ * @access public
202
+ * @param int $redirect_position
203
+ * @return boolean
204
+ */
205
+ function delete_redirect($redirect_position) {
206
+ // use nonces and check for permission http://www.garyc40.com/2010/03/5-tips-for-using-ajax-in-wordpress/
207
+
208
+ // handle ajax or regular request
209
+
210
+ // unserialize data
211
+
212
+ // find the redirect in the position that you want to delete and remove it
213
+
214
+ // serialize and save data
215
+
216
+ // if the request came from ajax, return true for the callback
217
+ // if it was traditional, redirect to the page with deleted=$redirect_position in url for save state
218
+ }
219
+ /**
220
+ * redirect function
221
+ * Read the list of redirects and if the current page
222
+ * is found in the list, send the visitor on her way
223
+ * @access public
224
+ * @return void
225
+ */
226
+ function redirect() {
227
  // this is what the user asked for (strip out home portion, case insensitive)
228
+ $userrequest = str_ireplace(get_option('home'),'',$this->get_address());
229
  $userrequest = rtrim($userrequest,'/');
230
 
231
  $redirects = get_option('301_redirects');
232
  if (!empty($redirects)) {
233
+
234
+ $wildcard = get_option('301_redirects_wildcard');
235
+ $do_redirect = '';
236
+
237
+ // compare user request to each 301 stored in the db
238
  foreach ($redirects as $storedrequest => $destination) {
239
+ // check if we should use regex search
240
+ if ($wildcard === 'true' && strpos($storedrequest,'*') !== false) {
241
+ // wildcard redirect
242
+
243
+ // don't allow people to accidentally lock themselves out of admin
244
+ if ( strpos($userrequest, '/wp-login') !== 0 && strpos($userrequest, '/wp-admin') !== 0 ) {
245
+ // Make sure it gets all the proper decoding and rtrim action
246
+ $storedrequest = str_replace('*','(.*)',$storedrequest);
247
+ $pattern = '/^' . str_replace( '/', '\/', rtrim( $storedrequest, '/' ) ) . '/';
248
+ $destination = str_replace('*','$1',$destination);
249
+ $output = preg_replace($pattern, $destination, $userrequest);
250
+ if ($output !== $userrequest) {
251
+ // pattern matched, perform redirect
252
+ $do_redirect = $output;
253
+ }
254
+ }
255
+ }
256
+ elseif(urldecode($userrequest) == rtrim($storedrequest,'/')) {
257
+ // simple comparison redirect
258
+ $do_redirect = $destination;
259
+ }
260
+
261
+ // redirect. the second condition here prevents redirect loops as a result of wildcards.
262
+ if ($do_redirect !== '' && trim($do_redirect,'/') !== trim($userrequest,'/')) {
263
+ // check if destination needs the domain prepended
264
+ if (strpos($do_redirect,'/') === 0){
265
+ $do_redirect = home_url().$do_redirect;
266
+ }
267
  header ('HTTP/1.1 301 Moved Permanently');
268
+ header ('Location: ' . $do_redirect);
269
  exit();
270
  }
271
  else { unset($redirects); }
273
  }
274
  } // end funcion redirect
275
 
276
+ /**
277
+ * getAddress function
278
+ * utility function to get the full address of the current request
279
+ * credit: http://www.phpro.org/examples/Get-Full-URL.html
280
+ * @access public
281
+ * @return void
282
+ */
283
+ function get_address() {
284
+ // return the full address
285
+ return $this->get_protocol().'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
286
+ } // end function get_address
287
+
288
+ function get_protocol() {
289
+ // Set the base protocol to http
290
+ $protocol = 'http';
291
+ // check for https
292
+ if ( isset( $_SERVER["HTTPS"] ) && strtolower( $_SERVER["HTTPS"] ) == "on" ) {
293
+ $protocol .= "s";
294
+ }
295
+
296
+ return $protocol;
297
+ } // end function get_protocol
298
 
299
  } // end class Simple301Redirects
300
 
311
  add_action('admin_menu', array($redirect_plugin,'create_menu'));
312
 
313
  // if submitted, process the data
314
+ if (isset($_POST['301_redirects'])) {
315
  $redirect_plugin->save_redirects($_POST['301_redirects']);
316
  }
317
  }
329
  $subject = str_replace($token,$replace,$subject);
330
  return $subject;
331
  }
332
+ }