WordPress.org

Ready to get started?Download WordPress

Forums

Simple 301 Redirects
Code Update - Added Regular Expressions (6 posts)

  1. jackie.shaffer
    Member
    Posted 2 years ago #

    I really enjoyed using this 301 redirect plugin, but I did have a need to include regular expressions in my redirects.

    Here is the code to update (would love to see something like this included in an update to the plugin, possible with more elegant coding)

    above line #263
    } // end class Simple301Redirects

    Add the following:

    //need to prepare url string for preg_match and replace
    
                   function prepare_preg_url($url, $encap_slash=false){
    
    			$url = str_replace('/', '\/', rtrim($url,'/'));
    			if( $encap_slash)
    			$url = '/'.$url.'/';
    
    			return $url;
    		}

    Then around line 180
    replace the function function redirect() with

    function redirect()
    
    		{
    
    			// this is what the user asked for (strip out home portion, case insensitive)
    
    			$userrequest = str_ireplace(get_option('home'),'',$this->getAddress());
    
    			$userrequest = rtrim($userrequest,'/');
    
    			$redirects = get_option('301_redirects');
    
    			if (!empty($redirects)) {
    
    				foreach ($redirects as $storedrequest => $destination) {
    
    				// compare user request to each 301 stored in the db
    				$preg_storedrequest=$this->prepare_preg_url($storedrequest, true);
    				$preg_destination = $this->prepare_preg_url($destination);
    
    				if (preg_match($preg_storedrequest, urldecode($userrequest))) {
    
    					$destination = preg_replace($preg_storedrequest , $preg_destination, urldecode($userrequest));
    
    						header ('HTTP/1.1 301 Moved Permanently');
    
    						header ('Location: ' . $destination);
    
    						exit();
    
    					}
    
    					else { unset($redirects); }
    
    				}
    
    			}
    
    		} // end funcion redirect

    Then you can add Regular Expressions into your 301 redirects, for example:

    Request: oldpage-([0-9]+)-([0-9]+)/
    Destination: /newpage.php?var1=\\1&var2=\\2

    http://wordpress.org/extend/plugins/simple-301-redirects/

  2. jackie.shaffer
    Member
    Posted 2 years ago #

    I have simplified the code, please use the below for the most recent version

    This small update will allow Regular Expressions into your 301 redirects, for example:

    Request: oldpage-([0-9]+)-([0-9]+)/
    Destination: /newpage.php?var1=$1&var2=$2

    around line 180
    replace the function function redirect() with

    function redirect()
    
    		{
    
    			// this is what the user asked for (strip out home portion, case insensitive)
    
    			$userrequest = str_ireplace(get_option('home'),'',$this->getAddress());
    
    			$userrequest = rtrim($userrequest,'/');
    
    			$redirects = get_option('301_redirects');
    
    			if (!empty($redirects)) {
    
    				foreach ($redirects as $storedrequest => $destination) {
    
    				// compare user request to each 301 stored in the db
    				$storedrequest='/'.str_replace('/', '\/', rtrim($storedrequest,'/')).'/';
    
    				if (preg_match($storedrequest, urldecode($userrequest))) {
    
    					$destination = preg_replace($storedrequest , $destination, urldecode($userrequest));
    
    						header ('HTTP/1.1 301 Moved Permanently');
    
    						header ('Location: ' . $destination);
    
    						exit();
    
    					}
    
    					else { unset($redirects); }
    
    				}
    
    			}
    
    		} // end funcion redirect
  3. rashmani
    Member
    Posted 2 years ago #

    Hi Jackie,

    I definitely need to use the plugin for my website (moved from a Joomla installation to WP) and I ultra-definitely need your update to avoid manually setting >400 rewrite rules.
    I've installed and tested the plugin with a single rule, now I should add your code.
    Before moving on tough, I have to admit my newbie-ness with regexp and ask for an advice.
    The question is: I need to replace the domain in old Joomla URLs with a new one, like rewriting

    http://www.lundici.it/?option=com_k2&view=item&id=106:shackleton&Itemid=73

    into

    http://vintage.lundici.it/?option=com_k2&view=item&id=106:shackleton&Itemid=73

    How would you proceed to write a regexp for this?

    Thanks for your code update!
    Cheers,
    rash*

  4. rashmani
    Member
    Posted 2 years ago #

    Hi again Jackie,

    ok, I found my way with regex, using this tool: http://rejex.heroku.com/ and reading a few docs.
    My regex doesn't fully work yet but I'm close.
    Thanks again!

    rash*

  5. rashmani
    Member
    Posted 2 years ago #

    Uhm,

    I'm stuck.
    I have a working regex, as far as http://rejex.heroku.com/ tells me, but I get not redirected where I want when I try my regex with the plugin.
    So my regex is:

    ^\/\?option\=com_k2&view\=item&id\=([0-9]+)\:([A-Za-z0-9-]*)\&Itemid\=([0-9]+)

    which matches this kind of URLs

    /?option=com_k2&view=item&id=329:la-posta-del-cuore-di-nt&Itemid=96

    also exposing groups

    1.329
    2.la-posta-del-cuore-di-nt
    3.96

    Within the plugin settings page, I then rewrite the URL as
    http://vintage.lundici.it/?option=com_k2&view=item&id=$1:$2&Itemid=$3
    but this doesn't seem to work.
    What am I doing wrong?

    Thanks for any help/hint you may provide.

    Cheers,
    rash*

  6. chadvonnau
    Member
    Posted 2 years ago #

    Thanks, Jackie. Works great for me.

    I'm migrating from an old site that was static html. Here's the regex I'm using:

    Request: /(.*).html
    Destination: /$1

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic