Evaluating a external rewrite rule before internal wordpress rewrite rule

I am been plucking out my hairs since last few days trying to solve this problem:

I want to convert my urls from format

Read More
http://example.com/prodsearch/category/tag1-tag2-tag3-tag4

to

http://example.com/prodsearch/index.php?tag=tag1+tag2+tag3+tag4&cat=category

to start with I added following rules to my .htaccess

RewriteRule ^prodsearch/(.*)/(.*)-(.*)$ prodsearch/$1/$2+$3 [L]
RewriteCond %{REQUEST_URI} !^prodsearch/(.*?)/(.*)-(.*)
RewriteRule ^prodsearch/(.*)/(.*)$ index.php?tag=$2&cat=$1 [R,QSA,L]

Here rule1 recursively replaces all ‘-‘ between tags by ‘+’
and rule2 does the actual rewrite once there are no more ‘-‘ left between tags (checked by RewriteCond)

These rules actually work, but the problem is they redirect (with url change) to new url pattern, I don’t want an explicit redirection. On removing ‘R’ flag from last rule, the whole thing stops working and I start getting 404 page in my wordpress install.

Can some one explain why this is happening and how to do this without explicit redirection.

Also I tried including these rules into wordpress, hoping when called by wordpress rules may work without redirection. I used following add_rewrite_rule() calls:

add_rewrite_rule('prodsearch/(.*)/(.*)-(.*)$','price-list/$1/$2+$3','top');
add_rewrite_rule('prodsearch/(.*)/(.*)$','index.php?tag=$matches[2]&cat=$matches[1]','top');

Now wordpress detects my first rule as external rule and flushes it to .htaccess file, this screws my execution order, now first rule 2 gets evaluated first(being part of internal wordpress rewrite rule set) and rule 1 gets executed later, hence again 404 page.

Is it possible to tell wordpress not to consider my first rule as a external rule and not to flush it to .htaccess.

Or does anyone has any idea to make this kind of rewrite work? Thanks a lot.

Related posts

Leave a Reply