简体   繁体   English

试图基于URL重定向

[英]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. 我正在搜索子字符串'/ product /',因此它需要是动态的(wordpress多站点,需要在所有站点上工作。

Tried this in functions.php.. not sure how/when to call it. 在functions.php中尝试过此方法。不知道如何/何时调用它。

 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. 在javascript中也尝试过,它可以在控制台中运行,但不能实时运行。

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. 在这一点上必须同意@wawa。 If you built this functionality into the template of /product/, you could then redirect users to the appropriate URL 如果将此功能内置到/ product /模板中,则可以将用户重定向到适当的URL

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

Found this SO answer 找到这个答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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