I'm new to WP programming and I am having trouble with what would be a simple problem in other environments.
I created a page (cdl-manual) using the normal WP admin UI. Then I added some functions to the theme functions.php file so I could use shortcodes for the page content. The functions look for a query parameter (jp_stn) on the URL and use that info to choose the content from a database table. In this case, the parameter value is the name of a US state. The content served is a page focused on that state.
All that is working now on my dev website.
http://blog.jobspetroleum.net/cdl-training/ lists a link to the CDL manual for each state. Those links are of the following form, as described above.
http://blog.jobspetroleum.net/cdl-manual/?jp_stn=arizona
Now I want to convert that state-specific query parameter URL to a user-friendly url. This is the new pattern.
http://blog.jobspetroleum.net/cdl-manual/arizona/
The obvious solution to me was to add a rewrite rule. After much fumbling, I found a plugin that let me add a custom rewrite rule directly to the .htaccess file. The plugin is "WP htaccess Control" and it works quite well. However, my rewrite rules are not working as they should. Here is the resulting .htaccess file after adding my rewrite rule.
# BEGIN WordPress
# WPhtc: Begin Custom htaccess
RewriteRule ^cdl-manual/([-a-z]+)/$ /cdl-manual/?jp_stn=$1 [L]
# WPhtc: End Custom htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
The new rule is in the Custom htaccess section. That one produces a 404 error when I test it with /cdl-manual/arizona/. To debug I tried changing the flags from [L] to [R,L].
That caused a redirect to http://blog.jobspetroleum.net/cdl-manual/?jp_stn=arizona. The page loaded content correctly but the URL was not rewritten. I conclude that the rule matches correctly. But without the R flag, I guess the rewrite doesn't work properly. The rule is correct according to http://htaccess.madewithlove.be/ and the rewritten URL is exactly what I want.
Does anyone know what is wrong here? Is my approach completely off? What's a better way to fix this? Thanks.
Edit 10/1/2014 - If it helps anyone, the page "CDL Manual" has only the content "[bigleaf_jobspet_cdlmanualbystate]" in it. This function, and other dependencies, are in the functions.php file in my child theme. This particular function looks for the jp_stn query parameter in GET, reads the state name, then pulls detailed content from tables I added to the database. It then returns the HTML for the named state.
Edit 10/2/2014 - Digging into the WP Codex I found a reference to the pagename query variable for index.php. So then I tried a few variations and came up with this rule that almost works.
RewriteRule ^cdl-manual/([-a-z]+)/$ ?pagename=cdl-manual&jp_stn=$1 [L]
This rule does load the cdl-manual page but ignores the state parameter value. Oddly, if I type the intended rewrite pattern into a browser, it loads exactly the desired page for the specified state.
http://blog.jobspetroleum.net/?pagename=cdl-manual&jp_stn=arizona