1

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
0

The rewrite rules are correct, but because you are modifying the $wp_rewrite directly, you aren't getting the benefit of being able to add the rule to the top of the stack using add_rewrite_rules.

You are on the right track with the location of the rule in the array, but rules at the top are checked first, and the first rule to match is used.

Replacing your two functions with the code below should handle the query_var and the rewrite rule + positioning.

/**
 * Adds rewrite rule for template variation paramater
 */
function gtp_add_variation_rewrite_rules() {
    add_rewrite_tag( '%variation%', '([0-9]*)' );
    add_rewrite_rule( 'products/(.+?)/variation/([0-9]*)/?$', 'index.php?post_type=products&products=$matches[1]&variation=$matches[2]', 'top' );
}

add_action( 'init', 'gtp_add_variation_rewrite_rules' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.