What I need
For the purpose of product page variation testing I am trying to rewrite my product page url to something like:
www.website.com/products/product-x/variation/1/
What I have done
I have added the query var as follows:
/**
* Adds variation query var
*/
function gtp_add_variation_query_vars( $vars ) {
$vars[] = 'variation';
return $vars;
}
add_filter( 'query_vars', 'gtp_add_variation_query_vars');
I have hooked into the generate_rewrite_rules
, to add a new rewrite rule as follows:
/**
* Adds rewrite rule for template variation paramater
*/
function gtp_add_variation_rewrite_rules() {
global $wp_rewrite;
// Rewrite rule with (v)ariation parameter
$new_rules['products/(.+?)/variation/([0-9]*)/?$'] = 'index.php?post_type=products&products=' . $wp_rewrite->preg_index( 1 ) . '&variation=' . $wp_rewrite->preg_index( 2 );
// Add new rewrite rules to rewrite rules array
$wp_rewrite->rules = $wp_rewrite->rules + $new_rules;
return $wp_rewrite->rules;
}
add_action( 'generate_rewrite_rules', 'gtp_add_variation_rewrite_rules' );
When I flush my rewrite rules I see the following rule at the bottom of the output of $wp_rewrite->rules
array added:
[products/(.+?)/variation/([0-9]*)/?$] => index.php?post_type=products&products=$matches[1]&variation=$matches[2]
The problem
But when I visit the the page: www.website.com/products/product-x/variation/1/
I get a 404.
The strange thing is that it works on my XAMPP localhost copy of the live site, but not on the live site itself. Could this be a server related problem? Or another rewrite rule (but this should not be a problem in my opinion, because the rule is at the bottom of the rewrite rules array)
Extra information:
I have set my permalink structure (under settings -> permalinks) to custom structure "/%category%/%postname%/"
.
My .htaccess file looks like this:
# BEGIN WordPress
<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