0

I am currently trying to create new rewrite rules for some taxonomies on a website and I encountered a problem that I just can't understand.

The rewrite rule works perfectly by itself but when I put it in a conditional statement using $_SERVER['REQUEST_URI'] it does not work.

What baffles me is that if I hardcode the value of $_SERVER['REQUEST_URI'] in a variable, and use this variable in the conditional statement, the rewrite rule works perfectly.

Here is some examples :

Let's say the current URL is :

domain.com/apparel/shirt/

In this scenario, the rewrite rule works perfectly :

$harcoded_uri = '/apparel/shirt/'; // (same content as $uri);

if (strpos($harcoded_uri, '/apparel/') !== false) {
    $regex = '^apparel/([^/]*)/?$';
    $location = 'index.php?type=$matches[1]';
    $priority = 'top';
    add_rewrite_rule( $regex, $location, $priority );
}

In this one, the rewrite rule does not work BUT "foo" is echoed :

$uri = $_SERVER['REQUEST_URI']; // = /apparel/shirt/  

if (strpos($uri, '/apparel/') !== false) {
    echo "foo";

    $regex = '^apparel/([^/]*)/?$';
    $location = 'index.php?type=$matches[1]';
    $priority = 'top';
    add_rewrite_rule( $regex, $location, $priority );
}

Is there a logical reason that when using $_SERVER['REQUEST_URI'] which contains the exact same content as my hardcoded variable the add_rewrite_rule() function seems to do nothing? I also can't figure an other way around.

=== EDIT #1 ===

As Milo pointed out in the comments, what I am trying to do with add_rewrite_rule() is not possible. I am posting here what I am trying to achieve. If anyone has an other idea, I will gladly take the input since I am now out of idea.

With the get parameters I can access this without any problem :

domain.com/apparel/?type=shirt
domain.com/apparel/?type=shirt&brand=nike
domain.com/apparel/?brand=nike
domain.com/apparel/?brand=nike&type=shirt

I would like to rewrite the urls to access the same content but with "clean" urls like :

domain.com/apparel/shirt/
domain.com/apparel/shirt/nike/
domain.com/apparel/nike/
domain.com/apparel/nike/shirt/

I am aware that for SEO reasons (duplicated content) that is not recommended but I have a solution for that. Keep in mind that this is a really dumbed down example. The urls could be much longer to access many more taxonomies (ex.: domain.com/apparel/shirt/nike/blue/small)

  • Since it is echoing 'foo' your condition is being met - so the problem lies somewhere inside your if statement. I am guessing there may be an issue with both the one that's working and the one that's not - specifically - you are using single quotes ' around index.php?type=$matches[1]. When you use single quotes PHP interprets literally so it is seeing dollar sign, matches[1] instead of using the actual variable. Try setting that line to double quotes " and see if that changes anything. – WebElaine Sep 19 '17 at 18:36
  • I just tested and my working example does not work when I use double quotes. I Might have clue here. – user2877718 Sep 19 '17 at 19:08
  • It could be that your $regex variable has a similar problem. If you take out your first scenario completely does the site behave the same way? – WebElaine Sep 19 '17 at 19:48
  • I am pretty sure that is the problem. I don't have time to test at the moment but I will report here tonight. Since using double quotes in the working scenario made it stop working, I know that the problem is in relation with that. Thank you very much for the help, I can't believe I did not spot the use of single quotes in $location ... – user2877718 Sep 19 '17 at 20:06
  • Your whole premise here is flawed. Rewrite rules are cached, you can't add a rule conditionally based on the current request because rules may be flushed on any request. – Milo Sep 19 '17 at 21:15

Your Answer

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

Browse other questions tagged or ask your own question.