Support » Plugin: Permalink Finder Plugin » Maintain custom query variables

Viewing 5 replies - 1 through 5 (of 5 total)
  • siddkb1986

    (@siddkb1986)

    So I think I kind of figured a pretty clean hack out, I edited the kpmg_permalink_fixer and have pasted the edited function below. But basically what I;m doing is, if there is a query string in the url, save it as a separate variable and append to all the redirect urls (wp_redirect).

    function kpg_permalink_fixer() {
    	$options=kpg_pf_get_options();
    	extract($options);
    	// fix request_uri on IIS
    	if (!array_key_exists('REQUEST_URI',$_SERVER)) {
    		$_SERVER['REQUEST_URI'] = substr($_SERVER['PHP_SELF'],1 );
    		if (isset($_SERVER['QUERY_STRING'])) {
    			$_SERVER['REQUEST_URI'].='?'.$_SERVER['QUERY_STRING'];
    		}
    	}
    	$plink = $_SERVER['REQUEST_URI'];
    	$query='';
    	if (strpos($plink,'?')!==false)
    		{
    		$query=substr($plink,strpos($plink,'?'));
    		$plink=substr($plink,0,strpos($plink,'?'));
    		}
    	if (strpos($plink,'#')!==false)  $plink=substr($plink,0,strpos($plink,'#'));
    	$flink = $plink; // flink has the page that was 404'd - not the basename
    	if (strpos($plink."\t","/\t")!==false)  $plink=substr($plink,0,strpos($plink."\t","/\t"));
    
    	//$plink=basename($plink); // plink now is the permalink part of the request.
    	// often I found this is wrong, I want to use the wholw taxonomy in the search
    
    	// remove the indexes if exist
    	if (strpos(strtolower($plink)."\t","/index.html\t")!==false) $plink=substr($plink."\t",0,strpos(strtolower($plink)."\t","/index.html\t"));
    	if (strpos(strtolower($plink)."\t","/index.htm\t")!==false) $plink=substr($plink."\t",0,strpos(strtolower($plink)."\t","/index.htm\t"));
    	if (strpos(strtolower($plink)."\t","/index.shtml\t")!==false) $plink=substr($plink."\t",0,strpos(strtolower($plink)."\t","/index.shtml\t"));
    	if (strpos(strtolower($plink)."\t","/default.asp\t")!==false) $plink=substr($plink."\t",0,strpos(strtolower($plink)."\t","/default.asp\t"));
    		// set up stats
    
    	// remove the server
    	$plink=str_ireplace(home_url(),'',$plink);
    	// now get rid of the slashes
    	$reason=$plink;
    	$plink=trim($plink);
    	$plink=trim($plink,'/');
    
    	$plink=str_replace('/','-',$plink); // this way the taxonomy becomes part of the search
    	$plink=str_replace('%20','-',$plink); // spaces are wrong
    
    	$ref='';
    	if (array_key_exists('HTTP_REFERER',$_SERVER)) $ref=$_SERVER['HTTP_REFERER'];
    	$ref=esc_url_raw($ref);
    	$ref=strip_tags($ref);
    	$ref=remove_accents($ref);
    	$ref=kpg_pf_really_clean($ref);
    
    	$agent='';
    	if (array_key_exists('HTTP_USER_AGENT',$_SERVER)) $agent=$_SERVER["HTTP_USER_AGENT"];
    	$agent=strip_tags($agent);
    	$agent=remove_accents($agent);
    	$agent=kpg_pf_really_clean($agent);
    	$agent=htmlentities($agent);
    	$request=$flink;
    	$request=esc_url_raw($request);
    	$request=strip_tags($request);
    	$request=remove_accents($request);
    	$request=kpg_pf_really_clean($request);
    
    	// set up stats
    	$r404=array();
    	$r404[0]=date('m/d/Y H:i:s',time() + ( get_option( 'gmt_offset' ) * 3600 ));
    	$r404[1]=$request;
    	$r404[2]=$ref;
    	$r404[3]=$agent;
    	$r404[4]=$_SERVER['REMOTE_ADDR'];
    	$r404[6]='';
    	// testing an ignore for the category
    	if (strpos($plink,"/category/")!==false) {
    		$r404[6]='/category/ is not redirected.';
    		kpg_find_permalink_error_log($options,$e404,$r404,$stats);
    		return;
    	}
    
    	// do not mess with robots trying to find wp-login.php and wp-signup.php
    	if (strpos($plink."\t","/wp-login.php\t")!==false||strpos($plink."\t","/wp-signup.php\t")!==false||strpos($plink."\t","/feed\t")!==false){
    		$r404[6]='$plink is probably a robot looking for exploits.';
    		kpg_find_permalink_error_log($options,$e404,$r404,$stats);
    		return;
    	}
    
    	// check for bypassed or generated files
    	if ($chkrobots=='Y'&&strpos(strtolower($plink)."\t","robots.txt\t")!==false) {
    		// looking for a robots.txt
    		// header out the .txt file
    		$r404[6]='display tobots.txt';
    		kpg_find_permalink_error_log($options,$e404,$r404,$stats);
    		header('HTTP/1.1 200 OK');
    		header('Content-Type: text/plain');
    		echo $robots;
    		exit();
    	}
    
    	if ($chkcrossdomain=='Y'&&strpos(strtolower($plink)."\t","crossdomain.xml\t")!==false) {
    		// looking for a robots.txt
    		// header out the .txt file
    		$r404[6]='display crossdomain.xml';
    		kpg_find_permalink_error_log($options,$e404,$r404,$stats);
    		header('HTTP/1.1 200 OK');
    		header('Content-Type: application/xml');
    		echo '<'.'?xml version="1.0"?'.">\r\n"; // because of ? and stuff need to echo this separate
    		?>
    <!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">
    <cross-domain-policy>
    <allow-access-from domain="<?php echo $_SERVER["HTTP_HOST"]; ?>" />
    </cross-domain-policy>
    		<?php
    		exit();
    	}
    
    	if ($chkicon=='Y'&&strpos(strtolower($plink)."\t","favicon.ico\t")!==false) {
    		// this only works if the favicon.ico is being redirected to wordpress on a 404
    		$f=dirname(__FILE__)."/includes/favicon.ico";
    		if (file_exists($f)) {
    			if (function_exists('header_remove'))header_remove();
    			ini_set('zlib.output_compression','Off');
    			header('HTTP/1.1 200 OK');
    			$r404[6]='display favicon.ico';
    			kpg_find_permalink_error_log($options,$e404,$r404,$stats);
    			header('Content-Type: image/x-icon');
    			header('Content-Disposition: attachment; filename="favicon.ico"');
    			readfile($f);
    			exit();
    		}
    	}
    /*
    	apple-touch-icon-57x57-precomposed.png
    	apple-touch-icon-57x57.png
    	apple-touch-icon-precomposed.png
    	apple-touch-icon.png
    */
    	if ($chkicon=='Y'&&(strpos(strtolower($plink)."\t","apple-touch-icon.png\t")!==false
    			||strpos(strtolower($plink)."\t","apple-touch-icon-57x57.png\t")!==false
    			||strpos(strtolower($plink)."\t","apple-touch-icon-precomposed.png\t")!==false
    			||strpos(strtolower($plink)."\t","apple-touch-icon.png\t")!==false
    		)
    	) {
    		// this only works if the favicon.ico is being redirected to wordpress on a 404
    		$f=dirname(__FILE__)."/includes/apple-touch-icon.png";
    		if (file_exists($f)) {
    			if (function_exists('header_remove'))header_remove();
    			ini_set('zlib.output_compression','Off');
    			$r404[6]='display apple-touch-icon.png';
    			kpg_find_permalink_error_log($options,$e404,$r404,$stats);
    			header('HTTP/1.1 200 OK');
    			header('Content-Type: image/png');
    			readfile($f);
    			exit();
    		}
    	}
    
    	if ($chksitemap=='Y'&&strpos(strtolower($plink)."\t","sitemap.xml\t")!==false) {
    		// if there is no sitemap, return the last 20 entries made
    		$r404[6]='display sitemap.xml';
    		kpg_find_permalink_error_log($options,$e404,$r404,$stats);
    		header('HTTP/1.1 200 OK');
    		header('Content-Type: application/xml');
    		$sitemap=kpg_pf_sitemap();
    		exit();
    	}
    	if ($chkdublin=='Y'&&strpos(strtolower($plink)."\t","dublin.rdf\t")!==false) {
    		// dublin.rdf is a little used method for robots to get more info about your site
    		$r404[6]='display dublin.rdf';
    		kpg_find_permalink_error_log($options,$e404,$r404,$stats);
    		header('HTTP/1.1 200 OK');
    		header('Content-Type: application/xml');
    		echo '<'.'?xml version="1.0"?'.'>'; // because of ? and stuff need to echo this separate
    	?>
     <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc= "http://purl.org/dc/elements/1.1/">
     <rdf:Description rdf:about="<?php echo get_home_url(); ?>">
     <dc:contributor><?php echo get_bloginfo('name'); ?></dc:contributor>
     <dc:date><?php echo date('Y-m-d',time() + ( get_option( 'gmt_offset' ) * 3600 )); ?></dc:date>
     <dc:description><?php echo get_bloginfo('description'); ?></dc:description>
     <dc:language><?php echo get_bloginfo('language'); ?></dc:language>
     <dc:publisher></dc:publisher>
     <dc:source><?php echo get_home_url(); ?></dc:source>
     </rdf:Description>
     </rdf:RDF>
    
    	<?php
    		exit();
    	}
    	if ($chkopensearch=='Y'&&(strpos(strtolower($plink)."\t","opensearch.xml\t")!==false||strpos(strtolower($plink)."\t","search.xml\t")!==false)) {
    		// search.xml may hel people search your site.
    		$r404[6]='display opensearch.xml';
    		kpg_find_permalink_error_log($options,$e404,$r404,$stats);
    		header('HTTP/1.1 200 OK');
    		header('Content-Type: application/xml');
    		echo '<'.'?xml version="1.0"?'.">\r\n"; // because of ? and stuff need to echo this separate
    	?>
     <OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
     <ShortName><?php echo get_bloginfo('name'); ?></ShortName>
     <Description>Search this site</Description>
     <Image>favicon.ico</Image>
     <Url type="text/html" template="<?php echo get_home_url(); ?>/seach"/>
     </OpenSearchDescription>
    
    	<?php
    		exit();
    	}
    	// some file types should not be included. these files are true 404s and WordPress can't fix that.
    	$ignoreTypes=array(
    	'jpg',
    	'gif',
    	'png',
    	'pdf',
    	'txt',
    	'asp',
    	'php',
    	'cfm',
    	'js',
    	'xml',
    	'php',
    	'mp3',
    	'wmv',
    	'css'
    	);
        foreach ($ignoreTypes as $it) {
    		if(strpos(strtolower($plink)."\t",'.'.$it."\t")!==false) {
    			$r404[6]="request for non WP file:.$it";
    			kpg_find_permalink_error_log($options,$e404,$r404,$stats);
    			return;
    		}
    	}
    
    	// santize to get rid of all odd characters, including cross browser scripts.
    	$plink=strtolower($plink); // make it case insensitive
    	// do some more cleanup
    	$plink=urldecode($plink);
    	$plink=strip_tags($plink);
    	$plink=remove_accents($plink);
    	$plink=kpg_pf_really_clean($plink);
    	$plink=str_replace('_','-',$plink);
    	$plink=str_replace(' ','-',$plink);
    	$plink=str_replace('%20','-',$plink);
    	$plink=str_replace('%22','-',$plink);
    	$plink=str_replace('/archive/','-',$plink);
    	$plink=sanitize_title_with_dashes($plink); // gets rid of some words that wordpress things are unimportant
    	// check if the incoming line needs a blogger fix
    	if (empty($plink)) {
    		// redirect back to siteurl
    		$flink=home_url();
    		$r404[5]=$flink;
    
    		$r404[6]="empty search, send to home";
    		kpg_find_permalink_fixed_log($options,$f404,$r404,$stats);
    		wp_redirect($flink.$query,(int)$kpg_pf_301); // let wp do it - more compatable.
    		exit();
    	}
    
    	if ($labels=='Y') {
    		if (strpos($flink,'/labels/')>0) {
    			$flink=str_replace('/labels/','/category/',$flink);
    			$flink=str_replace('.html','',$flink); // get dir of html and shtml at the end - don't need to search for these
    			$flink=str_replace('.shtml','',$flink);
    			$flink=str_replace('.htm','',$flink);
    			$flink=str_replace('_','-',$flink); // underscores should be dashes
    			$flink=str_replace('.','-',$flink); // periods should be dashes
    			$flink=str_replace(' ','-',$flink); // spaces are wrong
    			$flink=str_replace('%20','-',$flink); // spaces are wrong
    			$flink=str_replace('%22','-',$flink); // spaces are wrong
    			$flink=str_replace('"','-',$flink); // spaces are wrong
    			$r404[5]=$flink;
    			$r404[6]="Redirect /label/ to /category/";
    			kpg_find_permalink_fixed_log($options,$f404,$r404,$stats);
    			wp_redirect($flink.$query,(int)$kpg_pf_301); // let wp do it - more compatable.
    			exit();
    		}
    	}
    
    	// check to see if the user is coming in on a base default
    
    	// now figure if we need to fix a permalink
    	//echo "\r\n\r\n<!-- step 2 $find -->\r\n\r\n";
    	if ($find>0) {
    		$plink=str_replace('.html','',$plink); // get dir of html and shtml at the end - don't need to search for these
    		$plink=str_replace('.shtml','',$plink);
    		$plink=str_replace('.htm','',$plink);
    		$plink=str_replace('.php','',$plink);
    		// first check for the original slug - use the wordpress slug fixer on it.
    		if (strpos(strtolower($flink)."\t","/index.html\t")!==false) $flink=substr($flink."\t",0,strpos(strtolower($flink)."\t","/index.html\t"));
    		if (strpos(strtolower($flink)."\t","/index.htm\t")!==false) $flink=substr($flink."\t",0,strpos(strtolower($flink)."\t","/index.htm\t"));
    		if (strpos(strtolower($flink)."\t","/index.shtml\t")!==false) $flink=substr($flink."\t",0,strpos(strtolower($flink)."\t","/index.shtml\t"));
    		if (strpos(strtolower($flink)."\t","/default.asp\t")!==false) $flink=substr($flink."\t",0,strpos(strtolower($flink)."\t","/default.asp\t"));
    		$flink=basename($flink);
    		$flink=str_replace('.html','',$flink); // get dir of html and shtml at the end - don't need to search for these
    		$flink=str_replace('.shtml','',$flink);
    		$flink=str_replace('.htm','',$flink);
    		$flink=str_replace('_','-',$flink); // underscores should be dashes
    		$flink=str_replace('.','-',$flink); // periods should be dashes
    		$flink=str_replace(' ','-',$flink); // spaces are wrong
    		$flink=str_replace('%20','-',$flink); // spaces are wrong
    		$flink=str_replace('http://','',$flink);
    		$flink=str_replace('https://','',$flink);
    		$flink=sanitize_url($flink);
    		$flink=str_replace('http://','',$flink);
    		$flink=str_replace('https://','',$flink);
    		$flink=str_replace('%22','-',$flink); // spaces are wrong
    		$flink=str_replace('"','-',$flink); // spaces are wrong
    
    		// check for matches to slugs
    
    		$reason="Found exact slug $flink";
    		$ID=kpg_find_permalink_post_direct($flink);
    		// check - exact matches on flink
    		if ($ID==false) {
    			$ansa=kpg_find_permalink_post_exact($flink,$find ,$kpg_pf_numbs ,$kpg_pf_common ,$kpg_pf_short);
    			$ID=$ansa[0];
    			$cnt=$ansa[1];
    			$reason="Found $cnt exact word matches to slug $flink";
    		}
    		if ($ID==false&&$chkloose=='Y') {
    			$ansa=kpg_find_permalink_post_loose($flink,$find ,$kpg_pf_numbs ,$kpg_pf_common ,$kpg_pf_short);
    			$ID=$ansa[0];
    			$cnt=$ansa[1];
    			$reason="Found $cnt loose word matches to $flink";
    		}
    		if ($ID==false&&$chkfullurl=='Y') {
    			$ansa=kpg_find_permalink_post_exact($plink,$find ,$kpg_pf_numbs ,$kpg_pf_common ,$kpg_pf_short);
    			$ID=$ansa[0];
    			$cnt=$ansa[1];
    			$reason="Found $cnt exact word matches to $plink";
    		}
    		if ($ID==false&&$chkloose=='Y'&&$chkfullurl=='Y') {
    			$ansa=kpg_find_permalink_post_loose($plink,$find ,$kpg_pf_numbs ,$kpg_pf_common ,$kpg_pf_short);
    			$ID=$ansa[0];
    			$cnt=$ansa[1];
    			$reason="Found $cnt loose word matches to $plink";
    		}
    
    		if( $ID==false && $chkmetaphone=='Y')  {
    			// missed on regular words - try a metaphone search?? Only do it on original slug
    			$ansa=kpg_find_permalink_post_metaphone( $flink,$find ,$kpg_pf_numbs ,$kpg_pf_common ,$kpg_pf_short);
    			$ID=$ansa[0];
    			$cnt=$ansa[1];
    			if ($ID!==false)
    				$reason="Found $cnt metaphone 'sounds-like' word matches to $flink";
    			else
    				$reason="failed all searches";
    		}
    		if( $ID==false && $chkmetaphone=='Y' && $chkfullurl=='Y')  {
    			// missed on regular words - try a metaphone search?? Only do it on original slug
    			$ansa=kpg_find_permalink_post_metaphone( $plink,$find ,$kpg_pf_numbs ,$kpg_pf_common ,$kpg_pf_short);
    			$ID=$ansa[0];
    			$cnt=$ansa[1];
    			if ($ID!==false)
    				$reason="Found $cnt metaphone 'sounds-like' word matches to $plink";
    			else
    				$reason="failed all searches";
    		}
    		if( $ID!==false)  {
    			$r404[5]=get_permalink( $ID );
    			$r404[6]=$reason;
    			kpg_find_permalink_fixed_log($options,$f404,$r404,$stats);
    			wp_redirect(get_permalink( $ID ).$query,(int)$kpg_pf_301); // let wp do it - more compatable.
    			exit();
    		}
    	}
    	// still here, it must be a real 404, we should log it
    	$reason="Not found - slug:$flink, loose url:$plink";
    	//echo "\r\n\r\n<!-- step 5 -->\r\n\r\n";
    	$r404[6]=$reason;
    	kpg_find_permalink_error_log($options,$e404,$r404,$stats);
    
    	return; // end of permalink fixer
    }

    Hope this helps somebody else out there facing a similar problem.

    Plugin Author kpgraham

    (@kpgraham)

    I have to figure out how to add this to the next version. The place were you put this changed a little.

    I was going to release a new version soon, but I’ll wait until I get your code working.

    Thank you very much,

    Keith

    Plugin Author kpgraham

    (@kpgraham)

    There is a new beta version available on http://www.blogseye.com for the permalink finder. It has a bunch of changes including this one.

    I am copying it to my websites now and I’ll wait a week or so before I release it.

    The program is getting quite large and out of hand, so I am not yet confident that everything I changed will work.

    Keith

    siddkb1986

    (@siddkb1986)

    Hey Keith,
    Is the one on blogseye the updated one with the changes? I’d be happy to help you test. Let me know.

    Regards,
    Sidd

    Plugin Author kpgraham

    (@kpgraham)

    Sidd,

    The one on Blogseye has your changes in it. You can download it on the beta test page.

    Keith

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Maintain custom query variables’ is closed to new replies.