I’ve added a query string to my WordPress installation using the following code in my functions.php file:
function add_query_vars($aVars) {
$aVars[] = "username";
return $aVars;
}
add_filter('query_vars', 'add_query_vars');
function add_rewrite_rules($aRules) {
$aNewRules = array(
'([^/]+)/manage' => 'index.php?pagename=manage&username=$matches[1]',
'([^/]+)/settings' => 'index.php?pagename=settings&username=$matches[1]',
);
$aRules = $aNewRules + $aRules;
return $aRules;
}
add_filter('rewrite_rules_array', 'add_rewrite_rules');
However, I don’t want to load the page template if the username query string is empty, rather I’d like to redirect to the homepage. For instance:
website.com/myusername/manage
… would load a page template
website.com/manage
… would redirect home.
Can I do this using WordPress’ rewrite engine or should I take another approach?
Well, I would use this code: