Settings saved

'; } ?>

Simple 301 Redirects

expand_redirects(); ?>
Request Destination
example: /about.htm example: /about/
» Delete

/>

Documentation

Simple Redirects

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.

Example

Wildcards

To use wildcards, put an asterisk (*) after the folder name that you want to redirect.

Example

You can also use the asterisk in the destination to replace whatever it matched in the request if you like. Something like this:

Example

Or:

*/ function expand_redirects() { $redirects = get_option('301_redirects'); $output = ''; if (!empty($redirects)) { foreach ($redirects as $request => $destination) { $output .= ' » '; } } // end if return $output; } /** * save_redirects function * save the redirects from the options page to the database * @access public * @param mixed $data * @return void */ function save_redirects($data) { if ( !current_user_can('manage_options') ) { wp_die( 'You do not have sufficient permissions to access this page.' ); } check_admin_referer( 'save_redirects', '_s301r_nonce' ); $data = $_POST['301_redirects']; $redirects = array(); for($i = 0; $i < sizeof($data['request']); ++$i) { $request = trim( sanitize_text_field( $data['request'][$i] ) ); $destination = trim( sanitize_text_field( $data['destination'][$i] ) ); if ($request == '' && $destination == '') { continue; } else { $redirects[$request] = $destination; } } update_option('301_redirects', $redirects); if (isset($data['wildcard'])) { update_option('301_redirects_wildcard', 'true'); } else { delete_option('301_redirects_wildcard'); } } /** * redirect function * Read the list of redirects and if the current page * is found in the list, send the visitor on her way * @access public * @return void */ function redirect() { // this is what the user asked for (strip out home portion, case insensitive) $userrequest = str_ireplace(get_option('home'),'',$this->get_address()); $userrequest = rtrim($userrequest,'/'); $redirects = get_option('301_redirects'); if (!empty($redirects)) { $wildcard = get_option('301_redirects_wildcard'); $do_redirect = ''; // compare user request to each 301 stored in the db foreach ($redirects as $storedrequest => $destination) { // check if we should use regex search if ($wildcard === 'true' && strpos($storedrequest,'*') !== false) { // wildcard redirect // don't allow people to accidentally lock themselves out of admin if ( strpos($userrequest, '/wp-login') !== 0 && strpos($userrequest, '/wp-admin') !== 0 ) { // Make sure it gets all the proper decoding and rtrim action $storedrequest = str_replace('*','(.*)',$storedrequest); $pattern = '/^' . str_replace( '/', '\/', rtrim( $storedrequest, '/' ) ) . '/'; $destination = str_replace('*','$1',$destination); $output = preg_replace($pattern, $destination, $userrequest); if ($output !== $userrequest) { // pattern matched, perform redirect $do_redirect = $output; } } } elseif(urldecode($userrequest) == rtrim($storedrequest,'/')) { // simple comparison redirect $do_redirect = $destination; } // redirect. the second condition here prevents redirect loops as a result of wildcards. if ($do_redirect !== '' && trim($do_redirect,'/') !== trim($userrequest,'/')) { // check if destination needs the domain prepended if (strpos($do_redirect,'/') === 0){ $do_redirect = home_url().$do_redirect; } header ('HTTP/1.1 301 Moved Permanently'); header ('Location: ' . $do_redirect); exit(); } else { unset($redirects); } } } } // end funcion redirect /** * getAddress function * utility function to get the full address of the current request * credit: http://www.phpro.org/examples/Get-Full-URL.html * @access public * @return void */ function get_address() { // return the full address return $this->get_protocol().'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; } // end function get_address function get_protocol() { // Set the base protocol to http $protocol = 'http'; // check for https if ( isset( $_SERVER["HTTPS"] ) && strtolower( $_SERVER["HTTPS"] ) == "on" ) { $protocol .= "s"; } return $protocol; } // end function get_protocol } // end class Simple301Redirects } // end check for existance of class // instantiate $redirect_plugin = new Simple301Redirects(); if (isset($redirect_plugin)) { // add the redirect action, high priority add_action('init', array($redirect_plugin,'redirect'), 1); // create the menu add_action('admin_menu', array($redirect_plugin,'create_menu')); // if submitted, process the data if (isset($_POST['301_redirects'])) { add_action('admin_init', array($redirect_plugin,'save_redirects')); } } // this is here for php4 compatibility if(!function_exists('str_ireplace')){ function str_ireplace($search,$replace,$subject){ $token = chr(1); $haystack = strtolower($subject); $needle = strtolower($search); while (($pos=strpos($haystack,$needle))!==FALSE){ $subject = substr_replace($subject,$token,$pos,strlen($search)); $haystack = substr_replace($haystack,$token,$pos,strlen($search)); } $subject = str_replace($token,$replace,$subject); return $subject; } }