Custom Rewrite Rule Removing Query String
I'm trying (and failing) to use rewrite rules to redirect shorter/prettier URLs to the right page AND include values in the query string.
WP is installed at https://example.com, and the plugin creates a page called 'foo' which uses a value 'bar' passed in the query string to generate the content from custom database tables.
So an example full URL would be:
https://example.com/foo/?bar=XXXXXX
The 'bar' value would always be a string of between 6 and 8 alphanumeric chars with at least 1 dash(-).
I want to use rewrite rules (but am open to other suggestions) so that a shorter URL can be used, such as:
I have tried using add_rewrite_rule as follows:
function jr_rewrite_add_rewrites(){
add_rewrite_rule(
'f\/([[:ascii:]]+)\/?$',
'/index.php?pagename=foobar=$matches[1]',
'top'
);
}
add_action( 'init', 'jr_rewrite_add_rewrites');
And, when I flush the rewrite rules by clicking save on the Permalinks Settings page in the Dashboard, the rule is added to the htaccess file just fine:
# BEGIN WordPress
# The directives (lines) between `BEGIN WordPress` and `END WordPress` are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
IfModule mod_rewrite.c
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteRule ^f\/([[:ascii:]]+)\/?$ /index.php?pagename=foobar=$matches[1] [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
/IfModule
# END WordPress
However, when I try to access:
the page is just redirected to:
and is missing the needed bar=XXXXXX querystring so the relevant content does not load.
Can anyone please point out where I am going wrong wiht this or suggest an alternative?
Topic query-string url-rewriting plugins Wordpress
Category Web