I need the first sub-directory in my URL to rewrite to a querystringed URL if the category matches a program code ie: 4 upper case characters such as AUBU
For example, I would need:
http://www.georgiancollege.ca/devprograms/AUBU/
To rewrite to:
http://www.georgiancollege.ca/devprograms/index.php?page_id=16&major=AUBU
But I don't want it to affect current URLs such as
http://www.georgiancollege.ca/devprograms/a-to-z/
which should not be re-written to:
http://www.georgiancollege.ca/devprograms/index.php?page_id=16&major=a-to-z
Here is what I have so far which is not working at all. (based on: http://thereforei.am/2011/10/28/advanced-taxonomy-queries-with-pretty-urls/ )
function eg_add_rewrite_rules() {
global $wp_rewrite;
$new_rules = array(
'(.+)/?$' => 'index.php?page_id=16&major=' . $wp_rewrite->preg_index(1)
);
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
add_action( 'generate_rewrite_rules', 'eg_add_rewrite_rules' );
UPDATE: The code above now redirects to the proper page, but on that page I can't read the querystring in, likely because of the URL rewrite the querystring isn't on the final page...
So using,
$program = $_GET['major'];
Doesn't return the major code...
Looks like a hack for Apache url re-writing. I would go about this with Mod_Rewrite
As for your code you are matching with a regular expression that is not specific enough. You only want to match 4 uppercase characters? Maybe try this instead:
'devprograms/([A-Z]{4})/?$' => 'index.php?page_id=16&major=' . $wp_rewrite->preg_index(1)
Thanks for the input.
I've had to use a 'work around' solution because it just doesn't seem to want to work.
I tried using $wp_query->query_vars to get the querystring data as opposed to $_GET or $_POST like so:
if(isset($wp_query->query_vars['major'])) {
$program = urldecode($wp_query->query_vars['major']);
}
As suggested on: http://www.rlmseo.com/blog/passing-get-query-string-parameters-in-wordpress-url/ but the query_var for 'major' kept being non existent even though the pagename query var from the same URL rewrite did exist.
So I moved on...
Here is the new URL rewrite:
function add_rewrite_rules($aRules) {
$aNewRules = array('([A-Z]{4})/outline/?$' => 'index.php?pagename=programs&major=$matches[1]');
$aNewRules2 = array('([A-Z]{4})/?$' => 'index.php?pagename=programs&major=$matches[1]');
$aRules = $aNewRules + $aNewRules2 + $aRules;
return $aRules;
}
add_filter('rewrite_rules_array', 'add_rewrite_rules');
Since my URL rewrite is still keeping the program code in the URL just in a prettier way, I'm simply grabbing the REQUEST_URI and parsing out the program code like so:
$parts = explode('/',$_SERVER['REQUEST_URI']);
if (isset($_GET['major'])) {
$program = $_GET['major'];
} elseif ($parts[1] == 'devprograms') {
$program = $parts[2];
} else {
$program = get_the_title();
}
This accounts for both link styles /devprograms/BUSN and /devprograms/programs?major=BUSN
The only downside to this is I cannot have other pages on the install with 4 letter names such as 'test' or 'page' as they will get rewritten to the programs page with 'test' or 'page' as the program code, which won't be actual programs. That is an issue that is easy to work around when naming pages though.
Thanks,
Thomas