Dan
Dan

Reputation: 902

Rewrite rule doesn't use matches

A client's WordPress based web site has employee pages with urls like this:
/bio/john-doe/
/bio/nim-rod/
etc...

But on their printed brochures and business cards they want URLs like:
/nim.rod
and to have the site show the /bio/nim-rod/ page.

According to the client this used to work. There is a php file in the plugins folder that has what looks like rewrite rules for this capability (see below), but I can't get it to work.

I researched the problem and found this post on WordPress' support site but the suggested fix doesn't work:

Trying to troubleshoot the issue I added some test rules to the code, for example this rule does redirect /any.name to the /bio/nim-rod/ page,

$newrules['([a-zA-Z]+)\.([a-zA-Z]+)'] = 'index.php?pagename=nim-rod'; 

so the rewriting module is finding the custom rule and showing the /bio/nim-rod/ page, but the existing rule using the regex matches ends up showing the site's 404 page. So there seems to be something wrong with substituting the matched values from the regex pattern, but I can't find anything wrong with the syntax and I don't know how to troubleshoot it further. The existing pattern looks like it should work, shouldn't it?

Around 6 months ago the server had hardware problems and our former WordPress/PHP expert rebuilt the site and upgraded it to version 3.3.1. I don't know what the old version was, but it is possible that the new version broke this feature.

Are there troubleshooting tips you can point me to?
Are there any tools out there I can use to debug or inspect how the regex is working?

<?php
/**
 * @package Custom_Redirector
 * @version 1.0.0
 */
/*
Plugin Name: Custom Redirector
Plugin URI: http://wordpress.org/#
Description: 
Version: 1.0.0
Author URI: http://ma.tt/
*/

add_filter('rewrite_rules_array','wp_insertMyRewriteRules');
add_filter('query_vars','wp_insertMyRewriteQueryVars');
add_filter('init','flushRules');

// Remember to flush_rules() when adding rules
function flushRules(){
    global $wp_rewrite;
    $wp_rewrite->flush_rules();
}

// Adding a new rule
function wp_insertMyRewriteRules($rules)
{
    $newrules = array();
    $newrules['([a-zA-Z]+)\.([a-zA-Z]+)'] = 'index.php?pagename=$matches[1]-$matches[2]';
    return $newrules + $rules;
}

// Adding the id var so that WP recognizes it
function wp_insertMyRewriteQueryVars($vars)
{
    array_push($vars, 'id');
    return $vars;
}
?>

Upvotes: 0

Views: 321

Answers (1)

user1467267
user1467267

Reputation:

Try this;

([a-zA-Z]+)(?:\.|-)([a-zA-Z]+)

By the way, for this string it match two things index.php?pagename=nim-rod:

index.php?pagename=nim-rod

If you don't want that, then use;

([a-zA-Z]+)-([a-zA-Z]+)

Upvotes: 0

Related Questions