简体   繁体   中英

trying to redirect based on url

trying to watch for a specific url on the site, and based on that add a string to the url then redirect. I'm searching the substring '/product/' so it needs to be dynamic~ish (wordpress multisite, needs to work on all sites.

Tried this in functions.php.. not sure how/when to call it.

 function fancy_redirect() {
    $actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; 
    if ( (strpos($actual_link, '/product/') !== false) &&   (strpos($actual_link, '?start_customizing=yes') === false)) {
        header('Location: '. $actual_link . '?start_customizing=yes');
    }
}

Also tried it in javascript, this runs in console but not when live.

window.onload = function() {

  if(location.href.search("/product/") !== -1 && location.href.search("/?start_customizing=yes") === -1) {
    window.location = (location.href + "?start_customizing=yes");
  }

}

Going to have to agree with @wawa on this one. If you built this functionality into the template of /product/, you could then redirect users to the appropriate URL

header("Location: http://example.com/product/". $new_location);
die();

Found this SO answer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM